2014-10-30 85 views
1

我有以下實體(短版):休眠:NaturalId與繼承

GroupOfStudents:

@Entity 
@Table(name = "group_of_students") 
@Inheritance(strategy = InheritanceType.JOINED) 
public abstract class AGroupOfStudents extends AModel { 
} 

Centuria:

@Entity 
@Table(name = "cohort") 
@PrimaryKeyJoinColumn(name = "id") 
public class Cohort extends AGroupOfStudents { 

    @Column(nullable = false) 
    // @NaturalId <- here is the problem 
    private int number; 
} 

人羣:

@Entity 
@Table(name = "centuria") 
@PrimaryKeyJoinColumn(name = "id") 
public class Centuria extends AGroupOfStudents { 

    @Column(nullable = false) 
    // @NaturalId <- here is the problem 
    private int cohort; 


    @Column(nullable = false) 
    // @NaturalId <- here is the problem 
    private char maniple; 
} 

所以我有一個CourseLecture與GroupOfStudents。有不同的GroupOfStudents像Centuria或Cohort。但我希望隊列的數字字段是NaturalId。這將導致錯誤:

AnnotationException: @NaturalId only valid on root entity (or its @MappedSuperclasses) 

,但我爲什麼可以使用@NaturalId只有根實體?我怎樣才能解決這個問題,而不會破壞我的類繼承?

回答

0

好吧,我完全誤解了@NaturalId,主要目的是通過這個NaturalIds在表上啓用查詢,所以這個只對根實體工作纔有意義。

什麼是想要的是我的子實體一個簡單的唯一約束,這可以通過實現:

@Column(unique = true)單個列

@Table(name = "centuria", uniqueConstraints = { @UniqueConstraint(columnNames = { "cohort", "maniple", "letter" }) })多個列