- 부모클래스는 테이블과 매핑하지 않고 부모 클래스를 상속받는 자식 클래스에게 매핑 정보만 제공하고 싶을 때 사용한다.
- 추상클래스와 비슷하다.
@Entity는 실제 테이블과 매핑되지만@MappedSuperclass는 테이블과 매핑되지 않는다.
- 단순히 매핑 정보를 상속할 목적으로만 사용된다
- 연관관계를 재정의할때는 @AssociationOverride를 사용한다
@MappedSuperclass
@Data
public abstract class BaseEntity {
private Date createdAt;
private Date updatedAt;
}
@Data
@Entity
@Table(name = "Users")
public class User extends BaseEntity{
}
public User(String name, UserTypeEnum userType, Date now) {
this.name = name;
this.userType = userType;
super.setCreatedAt(now);
super.setUpdatedAt(now);
- 생성자에도 super를 사용해 부모클래스에 접근이 가능하도록 한다