2016-10-31 4 views
1

를 사용하여 상속을 매핑하는 방법 최대 절전 모드 어떻게 Hibernate에서 상속 클래스를 매핑 할 수 있습니다 : 예를 들어자바 주석

내가 추상 클래스의 그림과 두 자식 클래스 광장과 원형이있다. "수치"와 같이 하나의 테이블에 모두 매핑 할 수 있습니까?

것은 나는이

@Entity 
@Table(name = "figures") 
public abstract Figure{ 
} 

@Entity 
@Table(name = "figures") 
public class Square extends Figure{ 

} 

@Entity 
@Table(name = "figures") 
public class Circle extends Figure{ 

} 

뭔가를 시도했지만이 작업을 나던.

@Entity 
@Inheritance(strategy=InheritanceType.SINGLE_TABLE) 
@DiscriminatorColumn(name="type", discriminatorType=DiscriminatorType.STRING) 
@Table(name = "figures") 

DiscriminatorColumn이 객체가 어떤 종류의 알고 절전 모드에 의해 생성 된 새 열 것입니다 : 어떤 도움

감사합니다 :)

답변

2

당신이해야 할 무엇 상위 클래스에 주석을 추가합니다. 내 경우

나는 이름 "유형"

와 열을 생성 또한 해당 클래스

을 식별하는 데 사용 최대 절전 모드 값을 삽입 할 필요가 DiscriminatorValue에서 모든 자식 클래스

에 주석을

제 경우에는 String입니다. 그것은 그렇게 볼 수 있었다 귀하의 경우 (DiscriminatorColumn 주석의 discriminatorType) 그래서

@Entity 
@DiscriminatorValue(V) 

: http://www.javatpoint.com/hibernate-table-per-hierarchy-using-annotation-tutorial-example

: 당신은 여기에 더 많은 정보를 찾을 수 있습니다

@Entity 
@Inheritance(strategy=InheritanceType.SINGLE_TABLE) 
@DiscriminatorColumn(name="type", discriminatorType=DiscriminatorType.STRING) 
@Table(name = "figures") 
public class Figure{ 

} 

@Entity 
@DiscriminatorValue("S") 
public class Square extends Figure{ 

} 

@Entity 
@DiscriminatorValue("C") 
public class Circle extends Figure{ 

} 

관련 문제