001package model.entity; 002 003import lombok.Getter; 004import lombok.Setter; 005 006import javax.persistence.*; 007import java.io.Serializable; 008import java.util.Objects; 009 010 011/** 012 * Entità rappresentate il voto ad un commento 013 */ 014@Entity 015public class CommentVote implements Serializable { 016 017 @SuppressWarnings("JpaDataSourceORMInspection") //bug IDEA-223439 018 @Embeddable 019 public static class Id implements Serializable{ 020 021 @Getter 022 @Column(name = "user_id", nullable = false) 023 protected int userId; 024 025 @Getter 026 @Column(name = "comment_id", nullable = false) 027 protected int commentId; 028 029 protected Id(){} 030 031 public Id(int userId, int commentId) { 032 this.userId = userId; 033 this.commentId = commentId; 034 } 035 036 @Override 037 public boolean equals(Object o) { 038 if (this == o) return true; 039 if (!(o instanceof Id)) return false; 040 Id id = (Id) o; 041 return userId == id.userId && commentId == id.commentId; 042 } 043 044 @Override 045 public int hashCode() { 046 return Objects.hash(userId, commentId); 047 } 048 } 049 050 @Getter 051 @EmbeddedId 052 protected Id id = new Id(); 053 054 @Getter 055 @ManyToOne(optional = false) @MapsId("commentId") 056 protected Comment comment; 057 public void setComment(Comment comment){ 058 this.comment = comment; 059 this.id.commentId = comment.getId(); 060 } 061 062 @Getter 063 @ManyToOne(optional = false) @MapsId("userId") 064 protected User user; 065 public void setUser(User user){ 066 this.user = user; 067 this.id.userId = user.getId(); 068 } 069 070 @Setter @Getter 071 @Column(nullable = false) 072 protected Short vote; 073 074 protected CommentVote(){} 075 076 public CommentVote(User user, Comment comment, Short vote) { 077 this.user = user; 078 this.comment = comment; 079 this.vote = vote; 080 this.id = new Id(user.getId(), comment.getId()); 081 } 082 083 @Override 084 public boolean equals(Object o) { 085 if (this == o) return true; 086 if (!(o instanceof CommentVote)) return false; 087 CommentVote that = (CommentVote) o; 088 return id.equals(that.id); 089 } 090 091 @Override 092 public int hashCode() { 093 return Objects.hash(id); 094 } 095}