#16 board : entity - article comment

This commit is contained in:
haerong22
2022-08-05 00:51:32 +09:00
parent 940a841e0f
commit e79ca584e9
3 changed files with 71 additions and 3 deletions

View File

@@ -10,7 +10,9 @@ import org.springframework.data.annotation.LastModifiedDate;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;
@Getter
@ToString
@@ -38,6 +40,11 @@ public class Article {
@Setter
private String hashtag; // 해시태그
@OrderBy("id")
@OneToMany(mappedBy = "article", cascade = CascadeType.ALL)
@ToString.Exclude
private final Set<ArticleComment> articleComments = new LinkedHashSet<>();
@CreatedDate
@Column(nullable = false)
private LocalDateTime createdAt; // 생성일시

View File

@@ -1,14 +1,75 @@
package com.example.board.domain;
import java.time.LocalDateTime;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Objects;
@Getter
@ToString
@Table(indexes = {
@Index(columnList = "content"),
@Index(columnList = "createdAt"),
@Index(columnList = "createdBy")
})
@Entity
public class ArticleComment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Setter
@ManyToOne(optional = false)
private Article article; // 게시글 (ID)
@Setter
@Column(nullable = false, length = 500)
private String content; // 본문
@CreatedDate
@Column(nullable = false)
private LocalDateTime createdAt; // 생성일시
@CreatedBy
@Column(nullable = false, length = 100)
private String createdBy; // 생성자
@LastModifiedDate
@Column(nullable = false)
private LocalDateTime modifiedAt; // 수정일시
@LastModifiedBy
@Column(nullable = false, length = 100)
private String modifiedBy; //수정자
protected ArticleComment() {}
private ArticleComment(Article article, String content) {
this.article = article;
this.content = content;
}
public static ArticleComment of(Article article, String content) {
return new ArticleComment(article, content);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ArticleComment that)) return false;
return id != null && id.equals(that.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}

View File

@@ -10,8 +10,8 @@ logging:
spring:
datasource:
url: jdbc:mysql://localhost:3306/board
username: bobby
password: localhostTEST
username: root
password: 1234
driver-class-name: com.mysql.cj.jdbc.Driver
jpa: