1 package model.entity;
2
3 import lombok.Getter;
4 import lombok.Setter;
5
6 import javax.persistence.*;
7 import java.io.Serializable;
8 import java.util.Objects;
9
10
11
12
13
14 @Entity
15 public class CommentVote implements Serializable {
16
17 @SuppressWarnings("JpaDataSourceORMInspection")
18 @Embeddable
19 public static class Id implements Serializable{
20
21 @Getter
22 @Column(name = "user_id", nullable = false)
23 protected int userId;
24
25 @Getter
26 @Column(name = "comment_id", nullable = false)
27 protected int commentId;
28
29 protected Id(){}
30
31 public Id(int userId, int commentId) {
32 this.userId = userId;
33 this.commentId = commentId;
34 }
35
36 @Override
37 public boolean equals(Object o) {
38 if (this == o) return true;
39 if (!(o instanceof Id)) return false;
40 Id id = (Id) o;
41 return userId == id.userId && commentId == id.commentId;
42 }
43
44 @Override
45 public int hashCode() {
46 return Objects.hash(userId, commentId);
47 }
48 }
49
50 @Getter
51 @EmbeddedId
52 protected Id id = new Id();
53
54 @Getter
55 @ManyToOne(optional = false) @MapsId("commentId")
56 protected Comment comment;
57 public void setComment(Comment comment){
58 this.comment = comment;
59 this.id.commentId = comment.getId();
60 }
61
62 @Getter
63 @ManyToOne(optional = false) @MapsId("userId")
64 protected User user;
65 public void setUser(User user){
66 this.user = user;
67 this.id.userId = user.getId();
68 }
69
70 @Setter @Getter
71 @Column(nullable = false)
72 protected Short vote;
73
74 protected CommentVote(){}
75
76 public CommentVote(User user, Comment comment, Short vote) {
77 this.user = user;
78 this.comment = comment;
79 this.vote = vote;
80 this.id = new Id(user.getId(), comment.getId());
81 }
82
83 @Override
84 public boolean equals(Object o) {
85 if (this == o) return true;
86 if (!(o instanceof CommentVote)) return false;
87 CommentVote that = (CommentVote) o;
88 return id.equals(that.id);
89 }
90
91 @Override
92 public int hashCode() {
93 return Objects.hash(id);
94 }
95 }