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 /src/main/java/com/juick/data | |
parent | 9e2e2732d67ba0088b56804f7193e95b4ae46f22 (diff) |
JPA: fix relations, add new fields, add basic tests
Diffstat (limited to 'src/main/java/com/juick/data')
12 files changed, 69 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() { |