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只有根實體?我怎樣才能解決這個問題,而不會破壞我的類繼承?