【问题标题】:Is there multilevel structure in hibernate休眠中是否存在多级结构
【发布时间】:2020-07-11 16:12:08
【问题描述】:

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

Structure

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

【问题讨论】:

    标签: java spring spring-boot hibernate spring-mvc


    【解决方案1】:

    如果我们考虑到这些是 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 个类,QuestionAnswerSubQuestionAnswer

    这2个类都有一个discriminator列来区分它们引用的表(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;
    

    希望对你有所帮助。

    【讨论】:

    • 如果子问题是某些问题的可选字段,情况会怎样?
    猜你喜欢
    • 2018-08-03
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多