2015년 12월 25일 금요일

Mapped Superclasses and Non-Entity Superclasses

Mapped Superclasses

Entity는 entity가 아닌 mapping 정보와 persistent 상태를 가지고 있는 상위 클래스로부터 상속받을 수 있다.
즉, @Entity 어노테이션이 선언되어 있지않고, Java Persistence provider에 의해 entity로 맵핑되어 있는 것이 아닌 상위 클래스를 이야기한다.
이러한 상위클래스들은 흔히 공통으로 사용되는 상태와 mapping 정보를 여러 entity class들에게 상속시키는 것으로 사용된다.
이렇게 mapping된 상위 클래스들은 아래와 같은 어노테이션으로 선언된다.

javax.persistence.MappedSuperclass:

@MappedSuperclass
public class Employee {
    @Id
    protected Integer employeeId;
    ...
}
@Entity
public class FullTimeEmployee extends Employee {
    protected Integer salary;
    ...
}
@Entity
public class PartTimeEmployee extends Employee {
    protected Float hourlyWage;
    ...
}

맵핑된 상위클래스는 query를 날릴 수 없고, EntityManager나 Query 동작 안에서 사용될 수 없다.
맵핑된 상위클래스의 하위 entity 클래스를 사용하여 EntityManager나 Query를 사용해야 한다.
맵핑된 상위클래스는 entity 관계의 목표가 될 수 없다. 맵핑된 상위클래스는 abstract 혹은 기본 클래스가 되어질 수 있다.

맵핑된 상위클래스는 database 안에 상응하는 table을 가지지 않는다. 맵핑된 상위클래스로부터 상속받은 entity는 table mapping을 지정할 수 있다.
예를 들어, 위의 코드 샘플에서, FULLTIMEEMPLOYEE 와 PARTTIMEEMPLOYEE는 테이블을 가지지만, EMPLOYEE 테이블은 존재하지 않는다.

Non-Entity Superclasses

Entity는 entity가 아닌 abstract 혹은 concrete 상위클래스를 가질 수 있다. entity가 아닌 상위클래스는 persistence가 아니며,
non-entity 상위클래스로 부터 상속받은 어떠한 상태들도 persistence가 아니다. non-entity 상위클래스는 EntityManager나 Query 동작에 사용되어질 수 없다.
non-entity 상위클래스의 어떠한 mapping이나 relationship 어노테이션도 무시된다.

댓글 없음 :

댓글 쓰기