Skip to content

Commit 2cd4256

Browse files
committed
update CommentEntity - equals, hashcode
Create CommentRepository CreateCommentDto, and CommentDto
1 parent b8e9ea3 commit 2cd4256

4 files changed

Lines changed: 62 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package demo.codeexample.comment;
2+
3+
import lombok.Data;
4+
5+
import java.time.LocalDateTime;
6+
7+
@Data
8+
public class CommentDto {
9+
10+
private Long id;
11+
12+
private String content;
13+
14+
private Long writerId;
15+
16+
private LocalDateTime createdAt;
17+
18+
19+
}

src/main/java/demo/codeexample/comment/CommentEntity.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import lombok.Getter;
77
import lombok.NoArgsConstructor;
88
import lombok.Setter;
9+
import org.hibernate.proxy.HibernateProxy;
910

1011
import java.time.LocalDateTime;
12+
import java.util.Objects;
1113

1214
@Getter
1315
@Setter
@@ -40,5 +42,26 @@ protected void onCreate() {
4042
this.createdAt = LocalDateTime.now();
4143
}
4244

45+
public CommentEntity(String content, Long writerId,TaskEntity task, LocalDateTime createdAt) {
46+
this.content = content;
47+
this.writerId = writerId;
48+
this.task = task;
49+
this.createdAt = createdAt;
50+
}
51+
52+
@Override
53+
public final boolean equals(Object o) {
54+
if (this == o) return true;
55+
if (o == null) return false;
56+
Class<?> oEffectiveClass = o instanceof HibernateProxy ? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() : o.getClass();
57+
Class<?> thisEffectiveClass = this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass() : this.getClass();
58+
if (thisEffectiveClass != oEffectiveClass) return false;
59+
CommentEntity that = (CommentEntity) o;
60+
return getId() != null && Objects.equals(getId(), that.getId());
61+
}
4362

63+
@Override
64+
public final int hashCode() {
65+
return this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode() : getClass().hashCode();
66+
}
4467
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package demo.codeexample.comment;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
public interface CommentRepository extends JpaRepository<CommentEntity, Long> {
6+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package demo.codeexample.comment;
2+
3+
import jakarta.validation.constraints.NotBlank;
4+
import jakarta.validation.constraints.Size;
5+
import lombok.Data;
6+
7+
@Data
8+
public class CreateCommentDto {
9+
10+
@NotBlank(message = "Comment can't be empty.")
11+
@Size(max = 300, message = "Can't be more than 300 characters.")
12+
private String content;
13+
14+
}

0 commit comments

Comments
 (0)