001package model.entity; 002 003import lombok.Getter; 004 005import javax.persistence.*; 006import java.io.Serializable; 007import java.time.Instant; 008import java.util.Objects; 009 010/** 011 * Entità rappresentate una relazione "segui" tra un utente e una sezione 012 */ 013@Entity 014public class Follow implements Serializable { 015 016 @Embeddable 017 public static class Id implements Serializable{ 018 019 @Getter 020 @Column(name = "user_id", nullable = false) 021 protected int userId; 022 023 @Getter 024 @Column(name = "section_id", nullable = false) 025 protected int sectionId; 026 027 protected Id(){} 028 029 public Id(int userId, int sectionId){ 030 this.userId = userId; 031 this.sectionId = sectionId; 032 } 033 034 @Override 035 public boolean equals(Object o) { 036 if (this == o) return true; 037 if (!(o instanceof Id)) return false; 038 Id id = (Id) o; 039 return userId == id.userId && sectionId == id.sectionId; 040 } 041 042 @Override 043 public int hashCode() { 044 return Objects.hash(userId, sectionId); 045 } 046 } 047 048 @Getter 049 @EmbeddedId 050 protected Id id = new Id(); 051 052 @Getter 053 @ManyToOne(optional = false) @MapsId("userId") 054 protected User user; 055 public void setUser(User user){ 056 this.user = user; 057 this.id.userId = user.getId(); 058 } 059 060 @Getter 061 @ManyToOne(optional = false) @MapsId("sectionId") 062 protected Section section; 063 public void setSection(Section section){ 064 this.section = section; 065 this.id.sectionId = section.getId(); 066 } 067 068 @Getter 069 @Column(nullable = false, updatable = false, insertable = false) 070 protected Instant followDate; 071 072 protected Follow(){} 073 074 public Follow(User user, Section section){ 075 this.user = user; 076 this.section = section; 077 this.id = new Id(user.getId(), section.getId()); 078 } 079 080 081 @Override 082 public boolean equals(Object o) { 083 if (this == o) return true; 084 if (!(o instanceof Follow)) return false; 085 Follow follow = (Follow) o; 086 return id.equals(follow.id); 087 } 088 089 @Override 090 public int hashCode() { 091 return Objects.hash(id); 092 } 093}