【问题标题】:Hibernate Inheritance strategy=InheritanceType.JOINED & onetoMany with spring-data-jpaHibernate Inheritance strategy=InheritanceType.JOINED & onetoMany with spring-data-jpa
【发布时间】:2015-06-28 03:52:06
【问题描述】:

由于某种原因,我无法让 Hibernate Inheritance strategy=InheritanceType.JOINED 和 onetoMany 的组合正常工作。以下是实体。

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="OBJECT_TYPE")
public abstract class ExamObject {

        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        private Long id;

        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "examid", nullable = false)
        private Exam exam;
}

@Entity
@DiscriminatorValue("Q")
public class ExamQuestion extends ExamObject{

     private Integer questionNumber;

     private String questionDesc;
}

@实体

public class Exam {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer examid;

    private String examName;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "exam")
    private Set<ExamObject> object
}

我的 Spring Boot 启动类

@SpringBootApplication
public class ExamApp implements CommandLineRunner {
@Autowired
    private ExamQuestionRepository examQuestionRepository;

    @Autowired
    private ExamRepository examRepository;

    public static void main(String[] args) {
        SpringApplication.run(ExamApp.class, args);
    }


    @Override
    @Transactional
    public void run(String... arg0) throws Exception {
        Exam exam = new Exam();
        exam.setExamName("Exam1");
        examRepository.save(exam);

        String[] questions = new String[]{"Question1,Question2"};
        ArrayList<ExamQuestion> examQuestions = new ArrayList<ExamQuestion();
        int index = 0;
        for(String questionNoDesc: questions){
            index++;
            ExamQuestion examQuestion = new ExamQuestion();
            examQuestion.setQuestionDesc(questionNoDesc);
            examQuestion.setQuestionNumber(index);
            examQuestion.setExam(exam);
            examQuestions.add(examQuestion);
        }
        examQuestionRepository.save(examQuestions);

        Iterable<Exam> examGet = examRepository.findAll();
        for (Exam exam2: examGet) {
             System.out.println("Exam question is .. " +exam2.getObjects());
        }
    }
 }

问题是每当我打印"Exam question is .. "+exam2.getObjects() 时,我总是得到空值。我怎样才能让它工作?

【问题讨论】:

  • 这是意料之中的:您的代码从未将任何内容分配给exam.object,因此它为空。 exam2exam 都指向同一个对象。
  • 我已经用 onetoMany 映射了exam.object。所以我希望它在我检索考试时为我提供考试的考试对象列表。
  • 您在一个事务中完成所有操作。因此,您坚持的考试按原样存储在第一级(会话)缓存中。当您执行查询时,Hibernate 会返回缓存中已经存在的相同实例。保持对象图的一致性是您的责任:如果您设置了一个问题的考试,那么这个问题应该添加到考试的对象中。

标签: spring hibernate spring-boot spring-data spring-data-jpa


【解决方案1】:

可能是问题

@OneToMany(fetch = FetchType.LAZY, mappedBy = "exam") 私有集合对象

当您通过 LAZY 加载 FetchType.LAZY 获取任何东西时。这将从父表中获取所有对象,即此处的考试,但不会查询子/从属表的数据。

例如这里它不会命中 ExamObject 来获取它的数据,它只是用代理对象替换它,因此如果你查询这个对象,那么你会得到 null 作为结果。

FetchType.EAGER试试你的查询

【讨论】:

  • 上面评论的答案是对的。问题在于我对交易的理解。 fetch 类型没有给我带来问题。感谢您的回答。
【解决方案2】:

正如原始问题的评论中所解释的,问题在于对象图没有得到正确维护。以下函数的一行额外代码解决了该问题。 exam.setObjects(examQuestions);已添加

@Override
    @Transactional
    public void run(String... arg0) throws Exception {
        Exam exam = new Exam();
        exam.setExamName("Exam1");
        examRepository.save(exam);

        String[] questions = new String[]{"Question1,Question2"};
        ArrayList<ExamQuestion> examQuestions = new ArrayList<ExamQuestion();
        int index = 0;
        for(String questionNoDesc: questions){
            index++;
            ExamQuestion examQuestion = new ExamQuestion();
            examQuestion.setQuestionDesc(questionNoDesc);
            examQuestion.setQuestionNumber(index);
            examQuestion.setExam(exam);
            examQuestions.add(examQuestion);
        }
        examQuestionRepository.save(examQuestions);
        exam.setObjects(examQuestions);

        Iterable<Exam> examGet = examRepository.findAll();
        for (Exam exam2: examGet) {
             System.out.println("Exam question is .. " +exam2.getObjects());
        }
    }

【讨论】:

    猜你喜欢
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    相关资源
    最近更新 更多