76 lines
2.3 KiB
Java
76 lines
2.3 KiB
Java
package com.krylov.refound.entity;
|
|
|
|
import com.krylov.refound.util.TimeZoneContext;
|
|
import jakarta.persistence.Column;
|
|
import jakarta.persistence.Entity;
|
|
import jakarta.persistence.GeneratedValue;
|
|
import jakarta.persistence.GenerationType;
|
|
import jakarta.persistence.Id;
|
|
import jakarta.persistence.PrePersist;
|
|
import jakarta.persistence.Table;
|
|
import jakarta.persistence.UniqueConstraint;
|
|
import java.time.LocalDateTime;
|
|
import java.util.Objects;
|
|
import lombok.Data;
|
|
import lombok.Getter;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.Setter;
|
|
import lombok.ToString;
|
|
import org.hibernate.proxy.HibernateProxy;
|
|
|
|
@Entity
|
|
@Table(name = "chats", uniqueConstraints = {@UniqueConstraint(columnNames = {"user_one_id", "user_two_id"})})
|
|
@Getter
|
|
@Setter
|
|
@ToString
|
|
@RequiredArgsConstructor
|
|
public class Chat {
|
|
@Id
|
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
private Long id;
|
|
|
|
@Column(name = "user_one_id", nullable = false)
|
|
private Long userOneId; // всегда меньший id (для уникальности пары)
|
|
|
|
@Column(name = "user_two_id", nullable = false)
|
|
private Long userTwoId; // всегда больший id
|
|
|
|
@Column(name = "post_id")
|
|
private Long postId; // объявление, по поводу которого начат чат (опционально)
|
|
|
|
@Column(name = "created_at", nullable = false)
|
|
private LocalDateTime createdAt;
|
|
|
|
@PrePersist
|
|
protected void onCreate() {
|
|
createdAt = TimeZoneContext.now();
|
|
}
|
|
|
|
@Override
|
|
public final boolean equals(Object o) {
|
|
if (this == o) {
|
|
return true;
|
|
}
|
|
if (o == null) {
|
|
return false;
|
|
}
|
|
Class<?> oEffectiveClass =
|
|
o instanceof HibernateProxy proxy ? proxy.getHibernateLazyInitializer()
|
|
.getPersistentClass() : o.getClass();
|
|
Class<?> thisEffectiveClass =
|
|
this instanceof HibernateProxy proxy ? proxy.getHibernateLazyInitializer()
|
|
.getPersistentClass() : this.getClass();
|
|
if (thisEffectiveClass != oEffectiveClass) {
|
|
return false;
|
|
}
|
|
Chat chat = (Chat) o;
|
|
return getId() != null && Objects.equals(getId(), chat.getId());
|
|
}
|
|
|
|
@Override
|
|
public final int hashCode() {
|
|
return this instanceof HibernateProxy proxy ? proxy.getHibernateLazyInitializer()
|
|
.getPersistentClass().hashCode() : getClass().hashCode();
|
|
}
|
|
}
|