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