首页 > 解决方案 > 休眠中是否存在多级结构

问题描述

如果我必须提出具有子问题的问题并且所有问题和子问题都有各自的答案,如何设计豆子的以下结构。结构就像

结构

我如何在hibernate中设计这个结构来实现spring boot中的功能。

标签: javaspringspring-boothibernatespring-mvc

解决方案


如果我们考虑到这些是 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;

希望这对您有所帮助。


推荐阅读