82 lines
1.8 KiB
Java
Raw Normal View History

2023-03-30 21:12:38 +04:00
package com.webproglabs.lab1.lab34.model;
2023-03-20 20:07:29 +04:00
import javax.persistence.*;
import java.util.Objects;
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String text;
@ManyToOne()
private Profile owner;
2023-03-30 21:12:38 +04:00
@ManyToOne()
private Post post;
2023-03-20 20:07:29 +04:00
public Comment(){};
2023-03-30 21:12:38 +04:00
public Comment(String text, Profile owner, Post post) {
2023-03-20 20:07:29 +04:00
this.text = text;
this.owner = owner;
2023-03-30 21:12:38 +04:00
this.post = post;
2023-03-20 20:07:29 +04:00
}
public Long getId() {
return id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Profile getOwner() {
return owner;
}
public void setOwner(Profile owner) {
2023-03-30 21:12:38 +04:00
this.owner.getComments().remove(this);
2023-03-20 20:07:29 +04:00
this.owner = owner;
if (!owner.getComments().contains(this)) {
owner.getComments().add(this);
}
}
2023-03-30 21:12:38 +04:00
public Post getPost() {return post; }
public void setPost(Post post) {
this.post.getComments().remove(this);
this.post = post;
if (!post.getComments().contains(this)) {
post.getComments().add(this);
}
}
2023-03-20 20:07:29 +04:00
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Comment comment = (Comment) o;
return Objects.equals(id, comment.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
public String ToString() {
return "Comment{" +
"id=" + id +
", text='" + text + '\'' +
", owner='" + owner.ToString() + '\'' +
2023-03-30 21:12:38 +04:00
", post='" + post.ToString() + '\'' +
2023-03-20 20:07:29 +04:00
'}';
}
}