Mapped SuperClass

여러 테이블에서 공통 컬럼을 효율적으로 관리하는 MappedSuperclass의 사용법을 소개합니다.

여러 개의 테이블에서, 공통적인 컬럼들이 필요할 때 사용한다. (상속과는 관련 없다.) 예를 들면, 모든 테이블에 RegDate, ModifiedDate 필드가 필요한 상황이 있다.

 

package hellojpa.entity;

import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;

@MappedSuperclass
public abstract class BaseEntity {

    private String createdBy;
    private LocalDateTime createdDate;
    private String lastModifiedBy;
    private LocalDateTime lastModifiedDate;

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public LocalDateTime getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(LocalDateTime createdDate) {
        this.createdDate = createdDate;
    }

    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    public LocalDateTime getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(LocalDateTime lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }
}

 

이렇게 구성한 후에, 각각의 Entity 에서 상속받아서 사용하면 된다.


이것도 읽어보세요