공통되는 부분을 추출함으로써 코드의 중복을 없앨 수 있다.
추출하지 않으면 하나의 파일이 장황해질 수 있다.
1엔티티를 1데이터베이스를 매칭하기로 마음먹었으면 중복되게 작성하는 것도 나쁘지 않을 수 있다.
테이블단위 변경이 가능하고, 엔티티의 개별적 대응이 가능해서 변경에 유연하다.
추출한다는 것은 한 단계 더 들어가는 것이기 때문에 한눈에 확인이 어렵다는 단점이 있다.
공통필드를 묶은 클래스를 하나 만들어서, 엔티티에 필드를 추가한다.
@Embedded AAA aa;
class AAA { ... }
Embedded
와 마찬가지로 공통필드를 묶은 클래스를 하나 만든다.
해당 클래스에 MappedSuperclass
어노테이션을 붙인다.
그리고 해당 클래스를 사용해야하는, 원래 공통필드가 있던 엔티티에서 MappedSuperclass
클래스를 상속받는다.
package com.fastcampus.projectboard.domain;
import lombok.Getter;
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 org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;
@Getter
@ToString
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
public class AuditingFields {
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
@CreatedDate
@Column(nullable = false, updatable = false) private LocalDateTime createdAt; // 생성일시
@CreatedBy
@Column(nullable = false, length = 100, updatable = false) private String createdBy; // 생성자
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
@LastModifiedDate
@Column(nullable = false) private LocalDateTime modifiedAt; // 수정일시
@LastModifiedBy
@Column(nullable = false, length = 100) private String modifiedBy; // 수정자
}
package com.fastcampus.projectboard.domain;
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 org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;
@Getter
@ToString
@Table(indexes = {
@Index(columnList = "title"),
@Index(columnList = "hashtag"),
@Index(columnList = "createdAt"),
@Index(columnList = "createdBy")
})
@Entity
public class Article extends AuditingFields{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Setter @Column(nullable = false) private String title; // 제목
@Setter @Column(nullable = false, length = 10000) private String content; // 본문
@Setter private String hashtag; // 해시태그
@ToString.Exclude
@OrderBy("id")
@OneToMany(mappedBy = "article", cascade = CascadeType.ALL)
private final Set<ArticleComment> articleComments = new LinkedHashSet<>();
protected Article() {}
private Article(String title, String content, String hashtag) {
this.title = title;
this.content = content;
this.hashtag = hashtag;
}
public static Article of(String title, String content, String hashtag){
return new Article(title, content, hashtag);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Article)) return false;
Article article = (Article) o;
return id != null && id.equals(article.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}