2014-10-30 93 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" }) })多个列