java - 休眠中是否存在多级结构
解决方案
如果我们考虑到这些是 3 个不同的表,我认为这将是一个实体Question
@Entity
@Table("question")
public class Question
那么一个实体SubQuestion
@Entity
@Table("sub_question")
public class SubQuestion
与ManyToOne
联想
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="QUESTION_ID")
private Question question;
最后是一个通用类GenericAnswer
@Entity
@Table(name = "answer")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "question", discriminatorType = DiscriminatorType.STRING)
public class GenericAnswer {
将扩展 2 个类,QuestionAnswer
以及SubQuestionAnswer
这 2 个类有一个鉴别器列来区分它们所引用的表(question 或 sub_question)
@DiscriminatorValue("question")
@Entity
public class QuestionAnswer extends GenericAnswer {
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="QUESTION_ID")
private Question question;
和
@DiscriminatorValue("sub_question")
@Entity
public class SubQuestionAnswer extends GenericAnswer {
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="QUESTION_ID")
private SubQuestion subQuestion;
希望这对您有所帮助。