public @interface MappedSuperclass
Designates a class whose mapping information is applied to the entities that inherit from it. A mapped superclass has no separate table defined for it.
A class designated with the MappedSuperclass annotation can be mapped in the same way as an entity except that the mappings will apply only to its subclasses since no table exists for the mapped superclass itself. When applied to the subclasses the inherited mappings will apply in the context of the subclass tables. Mapping information may be overridden in such subclasses by using the AttributeOverride and AssociationOverride annotations or corresponding XML elements.
모든 클래스마다 직접 공통 필드를 작성할 수 있지만, 해당 필드를 가진 추상 클래스(혹은 일반 클래스)를 만들어 이를 다른 클래스들이 상속받도록 하는 것이 좀 더 효과적이다.공통된 필드를 사용하기 위한 상속을 받은 것이지, 부모와 자식의 관계 혹은 is-a 관계이기 때문에 상속을 받은 것이 아니다.
@MappedSuperClass
package com.example.demo.Entity;
import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import java.time.LocalDateTime;
@MappedSuperclass
@EntityListeners(value = AuditingEntityListener.class)
@Getter
abstract class BaseEntity {
@CreatedDate
@Column(name ="regdate", updatable = false)
private LocalDateTime regDate;
@LastModifiedDate
@Column(name="moddate")
private LocalDateTime modDate;
}
reference: https://velog.io/@rudwnd33/JPA-MappedSuperclass-vs-Embedded-Type
https://ttl-blog.tistory.com/132