@MappedSuperclass는 자식 클래스의 공통 매핑 컬럼 정보를 제공하는 어노테이션이다.
예를 들어 여러 테이블에서 생성 일시와 수정 일시 컬럼을 사용하려고 할 때, 생성되는 클래스에 동일한 날짜 필드를 매번 추가해줘야 한다.
이러한 중복을 공통화하기 위해 @MappedSuperclass를 사용한다. 해당 어노테이션이 붙은 클래스를 상속받아서 컬럼을 사용할 수 있다.
@MappedSuperclass
abstract class BaseEntity {
@Column(name = "created_at")
private LocalDateTime createdAt;
@Column(name = "modified_at")
private LocalDadteTime modifiedAt;
protected BaseEntity(LocalDateTime createdAt. LocalDateTime modifiedAt) {
this.createAt = createdAt;
this.modifiedAt = modifiedAt;
}
}
@Getter
@Entity
@Table(name = "article")
public class Article extends BaseEntity {
@Id
@Column(name = "article_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private String contents;
public Article(String title, String contents. LocalDateTime createdAt, LocalDateTime modifiedAt) {
super(createdAt, modifiedAt);
this.title = title;
this.contents = contents;
}
}
틀리거나 개선해야할 점이 있다면 댓글 부탁드립니다. 저에게 큰 힘이 됩니다:)
'🧑🏻💻Dev' 카테고리의 다른 글
ObjectMapper 기본 생성자 없이 역직렬화하기 (1) | 2023.01.23 |
---|---|
[리눅스 명령어] grep (0) | 2023.01.19 |
[리눅스 명령어] 파일 찾기 (0) | 2023.01.18 |
상속관계 매핑 (0) | 2022.12.06 |
연관관계 매핑 종류 (0) | 2022.12.05 |