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 +- 12 files changed, 69 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 (limited to 'src/main/java/com/juick') 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() { -- cgit v1.2.3