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 @Entity
14 public class PostVote implements Serializable {
15
16 @SuppressWarnings("JpaDataSourceORMInspection")
17 @Embeddable
18 public static class Id implements Serializable{
19
20 @Getter
21 @Column(name = "user_id", nullable = false)
22 protected int userId;
23
24 @Getter
25 @Column(name = "post_id", nullable = false)
26 protected int postId;
27
28 protected Id(){}
29
30 public Id(int userId, int postId) {
31 this.userId = userId;
32 this.postId = postId;
33 }
34
35 @Override
36 public boolean equals(Object o) {
37 if (this == o) return true;
38 if (!(o instanceof PostVote.Id)) return false;
39 PostVote.Id id = (PostVote.Id) o;
40 return userId == id.userId && postId == id.postId;
41 }
42
43 @Override
44 public int hashCode() {
45 return Objects.hash(userId, postId);
46 }
47 }
48
49 @Getter
50 @EmbeddedId
51 protected PostVote.Id id = new PostVote.Id();
52
53 @Getter
54 @ManyToOne(optional = false) @MapsId("postId")
55 protected Post post;
56 public void setPost(Post post){
57 this.post = post;
58 this.id.postId = post.getId();
59 }
60
61 @Getter
62 @ManyToOne(optional = false) @MapsId("userId")
63 protected User user;
64 public void setUser(User user){
65 this.user = user;
66 this.id.userId = user.getId();
67 }
68
69 @Setter
70 @Getter
71 @Column(nullable = false)
72 protected Short vote;
73
74 protected PostVote(){}
75
76 public PostVote(User user, Post post, Short vote) {
77 this.user = user;
78 this.post = post;
79 this.vote = vote;
80 this.id = new PostVote.Id(user.getId(), post.getId());
81 }
82
83 @Override
84 public boolean equals(Object o) {
85 if (this == o) return true;
86 if (!(o instanceof PostVote)) return false;
87 PostVote that = (PostVote) o;
88 return id.equals(that.id);
89 }
90
91 @Override
92 public int hashCode() {
93 return Objects.hash(id);
94 }
95 }