From 9436d4927dc5035c83620bdd1dd34600370dce56 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 1 Jul 2024 04:10:37 +0300 Subject: JPA: fix relations, add new fields, add basic tests --- .../java/com/juick/data/MessagesRepository.java | 4 +- src/main/java/com/juick/data/UsersRepository.java | 4 +- .../data/entities/BaseMessagePropertyEntity.java | 2 +- .../java/com/juick/data/entities/EmailEntity.java | 2 +- .../com/juick/data/entities/FacebookEntity.java | 4 +- .../com/juick/data/entities/MessageEntity.java | 24 +++++------ .../java/com/juick/data/entities/ReplyEntity.java | 15 +++++-- .../java/com/juick/data/entities/TagEntity.java | 40 +++++++++++++++--- .../com/juick/data/entities/TagNameEntity.java | 47 ---------------------- .../com/juick/data/entities/TelegramEntity.java | 2 +- .../java/com/juick/data/entities/TextEntity.java | 42 ------------------- .../java/com/juick/data/entities/VKEntity.java | 4 +- src/main/resources/application.properties | 1 + .../java/com/juick/server/tests/ServerTests.java | 30 ++++++++++++++ 14 files changed, 100 insertions(+), 121 deletions(-) delete mode 100644 src/main/java/com/juick/data/entities/TagNameEntity.java delete mode 100644 src/main/java/com/juick/data/entities/TextEntity.java 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 { +public interface MessagesRepository extends JpaRepository { } 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 { +public interface UsersRepository extends JpaRepository { Optional findById(Integer uid); UserEntity findByName(String name); Optional 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 tags = new HashSet<>(); - @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "message") - private Set replies = new HashSet<>(); - @OneToOne(cascade = CascadeType.ALL) - @PrimaryKeyJoinColumn - private TextEntity textEntity; + @OneToMany(mappedBy = "message") + private Set 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 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 getMessages() { + return messages; + } + + public void setMessages(Set 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 . - */ - -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 . - */ - -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)); + } } -- cgit v1.2.3