我有以下实体(短版):
学生组:
@Entity
@Table(name = "group_of_students")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class AGroupOfStudents extends AModel {
}
百人队:
@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;
}
所以我有一个 GroupOfStudents 的 CourseLecture。有不同的 GroupOfStudents,例如 Centuria 或 Cohort。但我希望 Cohort 的 number 字段是 NaturalId。这将导致错误:
AnnotationException: @NaturalId only valid on root entity (or its @MappedSuperclasses)
但是为什么我只能在根实体上使用 @NaturalId 呢?如何在不破坏类继承的情况下解决这个问题?