diff options
author | Vitaly Takmazov | 2024-07-01 04:10:37 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2024-07-01 04:10:37 +0300 |
commit | 9436d4927dc5035c83620bdd1dd34600370dce56 (patch) | |
tree | 2443779dddd23288e0f71b852293d736cf5db942 | |
parent | 9e2e2732d67ba0088b56804f7193e95b4ae46f22 (diff) |
JPA: fix relations, add new fields, add basic tests
14 files changed, 100 insertions, 121 deletions
diff --git a/src/main/java/com/juick/data/MessagesRepository.java b/src/main/java/com/juick/data/MessagesRepository.java index 8207ffcb..7f42c454 100644 --- a/src/main/java/com/juick/data/MessagesRepository.java +++ b/src/main/java/com/juick/data/MessagesRepository.java @@ -18,7 +18,7 @@ package com.juick.data; import com.juick.data.entities.MessageEntity; -import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.data.jpa.repository.JpaRepository; -public interface MessagesRepository extends PagingAndSortingRepository<MessageEntity, String> { +public interface MessagesRepository extends JpaRepository<MessageEntity, Integer> { } diff --git a/src/main/java/com/juick/data/UsersRepository.java b/src/main/java/com/juick/data/UsersRepository.java index 640fd75a..e2fcc0e8 100644 --- a/src/main/java/com/juick/data/UsersRepository.java +++ b/src/main/java/com/juick/data/UsersRepository.java @@ -18,7 +18,7 @@ package com.juick.data; import com.juick.data.entities.UserEntity; -import org.springframework.data.repository.CrudRepository; +import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.Collection; @@ -26,7 +26,7 @@ import java.util.Optional; import java.util.stream.Stream; @Repository -public interface UsersRepository extends CrudRepository<UserEntity, String> { +public interface UsersRepository extends JpaRepository<UserEntity, String> { Optional<UserEntity> findById(Integer uid); UserEntity findByName(String name); Optional<UserEntity> findByNameAndPassword(String name, String password); diff --git a/src/main/java/com/juick/data/entities/BaseMessagePropertyEntity.java b/src/main/java/com/juick/data/entities/BaseMessagePropertyEntity.java index 934cce92..90c26dbb 100644 --- a/src/main/java/com/juick/data/entities/BaseMessagePropertyEntity.java +++ b/src/main/java/com/juick/data/entities/BaseMessagePropertyEntity.java @@ -21,7 +21,7 @@ import jakarta.persistence.*; @MappedSuperclass public class BaseMessagePropertyEntity { - @ManyToOne(fetch = FetchType.LAZY) + @ManyToOne @JoinColumn(name = "message_id") private MessageEntity message; public MessageEntity getMessage() { diff --git a/src/main/java/com/juick/data/entities/EmailEntity.java b/src/main/java/com/juick/data/entities/EmailEntity.java index 4e71adc5..e13ca772 100644 --- a/src/main/java/com/juick/data/entities/EmailEntity.java +++ b/src/main/java/com/juick/data/entities/EmailEntity.java @@ -24,7 +24,7 @@ import jakarta.persistence.*; public class EmailEntity extends BaseUserPropertyEntity { @Id @Column(name = "email") - @GeneratedValue(strategy = GenerationType.AUTO) + @GeneratedValue(strategy = GenerationType.IDENTITY) private String email; public String getEmail() { diff --git a/src/main/java/com/juick/data/entities/FacebookEntity.java b/src/main/java/com/juick/data/entities/FacebookEntity.java index 75bee8d8..744848d9 100644 --- a/src/main/java/com/juick/data/entities/FacebookEntity.java +++ b/src/main/java/com/juick/data/entities/FacebookEntity.java @@ -23,8 +23,8 @@ import jakarta.persistence.*; @Table(name = "facebook") public class FacebookEntity extends BaseUserPropertyEntity { @Id - @Column(name = "fb_id", columnDefinition = "bigint(20)") - @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "fb_id") + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long facebookId; public Long getFacebookId() { diff --git a/src/main/java/com/juick/data/entities/MessageEntity.java b/src/main/java/com/juick/data/entities/MessageEntity.java index 416334d6..21896fb9 100644 --- a/src/main/java/com/juick/data/entities/MessageEntity.java +++ b/src/main/java/com/juick/data/entities/MessageEntity.java @@ -30,24 +30,24 @@ public class MessageEntity implements Serializable { @Column(name = "message_id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; - @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "message") + @ManyToMany + @JoinTable(name = "messages_tags", + joinColumns = @JoinColumn(name = "message_id"), + inverseJoinColumns = @JoinColumn(name = "tag_id") + ) private Set<TagEntity> tags = new HashSet<>(); - @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "message") - private Set<ReplyEntity> replies = new HashSet<>(); - @OneToOne(cascade = CascadeType.ALL) - @PrimaryKeyJoinColumn - private TextEntity textEntity; + @OneToMany(mappedBy = "message") + private Set<ReplyEntity> replies = new HashSet<>(); - @ManyToOne(cascade = CascadeType.ALL) + @ManyToOne(cascade = CascadeType.MERGE) private UserEntity user; @Column(name = "ts") private Instant ts; - public MessageEntity() { - this.textEntity = new TextEntity(); - } + @Column(name = "txt") + private String text; public Integer getId() { return id; @@ -74,11 +74,11 @@ public class MessageEntity implements Serializable { } public String getText() { - return textEntity.getText(); + return text; } public void setText(String text) { - this.textEntity.setText(text); + this.text = text; } public UserEntity getUser() { diff --git a/src/main/java/com/juick/data/entities/ReplyEntity.java b/src/main/java/com/juick/data/entities/ReplyEntity.java index e7f3d053..bc0795cf 100644 --- a/src/main/java/com/juick/data/entities/ReplyEntity.java +++ b/src/main/java/com/juick/data/entities/ReplyEntity.java @@ -24,11 +24,12 @@ import jakarta.persistence.*; public class ReplyEntity extends BaseMessagePropertyEntity { @Id @Column(name = "reply_id") - @GeneratedValue(strategy = GenerationType.AUTO) + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long replyId; @Column(name = "txt") - private - String text; + private String text; + @ManyToOne(cascade = CascadeType.MERGE) + private UserEntity user; public String getText() { return text; @@ -45,4 +46,12 @@ public class ReplyEntity extends BaseMessagePropertyEntity { public void setReplyId(Long replyId) { this.replyId = replyId; } + + public UserEntity getUser() { + return user; + } + + public void setUser(UserEntity user) { + this.user = user; + } } diff --git a/src/main/java/com/juick/data/entities/TagEntity.java b/src/main/java/com/juick/data/entities/TagEntity.java index 6fbc6987..e5c5266d 100644 --- a/src/main/java/com/juick/data/entities/TagEntity.java +++ b/src/main/java/com/juick/data/entities/TagEntity.java @@ -19,14 +19,42 @@ package com.juick.data.entities; import jakarta.persistence.*; +import java.util.Set; + @Entity -@Table(name = "messages_tags") -public class TagEntity extends BaseMessagePropertyEntity { +@Table(name = "tags") +public class TagEntity { @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "tag_id") - @GeneratedValue(strategy = GenerationType.AUTO) private int id; - @OneToOne(cascade = CascadeType.ALL) - @PrimaryKeyJoinColumn - private TagNameEntity tagName; + @Column(name = "name") + private String name; + + @ManyToMany(mappedBy = "tags") + private Set<MessageEntity> messages; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Set<MessageEntity> getMessages() { + return messages; + } + + public void setMessages(Set<MessageEntity> messages) { + this.messages = messages; + } } diff --git a/src/main/java/com/juick/data/entities/TagNameEntity.java b/src/main/java/com/juick/data/entities/TagNameEntity.java deleted file mode 100644 index 98d3bbea..00000000 --- a/src/main/java/com/juick/data/entities/TagNameEntity.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2008-2024, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -package com.juick.data.entities; - -import jakarta.persistence.*; - -@Entity -@Table(name = "tags") -public class TagNameEntity { - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - @Column(name = "tag_id") - private int id; - @Column(name = "name") - private String name; - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/src/main/java/com/juick/data/entities/TelegramEntity.java b/src/main/java/com/juick/data/entities/TelegramEntity.java index a592f635..453b8c11 100644 --- a/src/main/java/com/juick/data/entities/TelegramEntity.java +++ b/src/main/java/com/juick/data/entities/TelegramEntity.java @@ -24,7 +24,7 @@ import jakarta.persistence.*; public class TelegramEntity extends BaseUserPropertyEntity { @Id @Column(name = "tg_id") - @GeneratedValue(strategy = GenerationType.AUTO) + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long identifier; public Long getIdentifier() { diff --git a/src/main/java/com/juick/data/entities/TextEntity.java b/src/main/java/com/juick/data/entities/TextEntity.java deleted file mode 100644 index c96adb96..00000000 --- a/src/main/java/com/juick/data/entities/TextEntity.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (C) 2008-2024, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - -package com.juick.data.entities; - -import jakarta.persistence.*; -import java.io.Serializable; - -@Entity -@Table(name = "messages_txt") -public class TextEntity implements Serializable { - - @Id - @Column(name = "message_id") - @GeneratedValue(strategy = GenerationType.AUTO) - private int id; - - @Column(name = "txt") - private String text; - - public String getText() { - return text; - } - - public void setText(String text) { - this.text = text; - } -} diff --git a/src/main/java/com/juick/data/entities/VKEntity.java b/src/main/java/com/juick/data/entities/VKEntity.java index 04aa55eb..5388c855 100644 --- a/src/main/java/com/juick/data/entities/VKEntity.java +++ b/src/main/java/com/juick/data/entities/VKEntity.java @@ -23,8 +23,8 @@ import jakarta.persistence.*; @Table(name = "vk") public class VKEntity extends BaseUserPropertyEntity { @Id - @Column(name = "vk_id", columnDefinition = "bigint(20)") - @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "vk_id") + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long vkId; public Long getVkId() { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 112b7ae9..21c9f17a 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -9,6 +9,7 @@ spring.h2.console.enabled=false spring.datasource.generate-unique-name=false spring.jpa.generate-ddl=false spring.jpa.hibernate.ddl-auto=none +spring.jpa.open-in-view=false spring.flyway.enabled=false spring.sql.init.platform=h2 spring.cache.type=simple diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index a942323c..a9f767f7 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -30,6 +30,10 @@ import com.gargoylesoftware.htmlunit.html.HtmlPage; import com.github.scribejava.apis.AppleClientSecretGenerator; import com.jayway.jsonpath.JsonPath; import com.juick.*; +import com.juick.data.MessagesRepository; +import com.juick.data.UsersRepository; +import com.juick.data.entities.MessageEntity; +import com.juick.data.entities.ReplyEntity; import com.juick.model.Tag; import com.juick.model.*; import com.juick.server.MockNotificationListener; @@ -196,6 +200,10 @@ public class ServerTests { @Inject private ProfileUriBuilder profileUriBuilder; @Inject + private MessagesRepository messagesRepository; + @Inject + private UsersRepository usersRepository; + @Inject private WebApp webApp; @Value("${ap_base_uri:http://localhost:8080/}") private String baseUri; @@ -2717,4 +2725,26 @@ public class ServerTests { var truncated = StringUtils.abbreviate(text, "…", 47); assertThat(truncated, is(expected)); } + @Test + @Transactional + public void jpaMessageTests() { + var user = usersRepository.findByName(ugnichName); + var message = new MessageEntity(); + message.setUser(user); + message.setText("Hello, JPA!"); + message.setTimestamp(Instant.now()); + message = messagesRepository.save(message); + var msg = messagesService.getMessage(message.getId()); + assertThat(msg.get().getText(), is(notNullValue())); + assertThat(msg.get().getText(), is (message.getText())); + } + @Test + @Transactional + public void jpaReplyTests() { + var mid = messagesService.createMessage(ugnich.getUid(), "new message", null, Set.of()); + var rid = messagesService.createReply(mid, 0, ugnich, "reply 1", null); + var message = messagesRepository.getReferenceById(mid); + var replies = message.getReplies(); + assertThat(replies.size(), is(1)); + } } |