Hibernate抛出MultipleBagFetchException [英] MultipleBagFetchException thrown by Hibernate

查看:61
本文介绍了Hibernate抛出MultipleBagFetchException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的存储库层中有一个选项来渴望加载实体,所以我尝试添加一种方法来渴望加载具有所有关系的问题实体,但是它抛出MultipleBagFetchException.我怎样才能解决这个问题?我正在使用Hibernate 4.16.

I want to have an option in my repository layer to eager load entites, so I tried adding a method that should eager load a question entity with all the relationships, but it throws MultipleBagFetchException. How can I fix this? I am using Hibernate 4.16.

@NamedQuery(name = Question.FIND_BY_ID_EAGER, query = "SELECT q FROM Question q LEFT JOIN FETCH q.answers LEFT JOIN FETCH q.categories LEFT JOIN FETCH q.feedback LEFT JOIN FETCH q.participant WHERE q.id = :id"),

如何获得最初是延迟加载的问题对象,以期热切地加载所有关系?

How do I get a question object which is initially lazy loaded, to be eager loaded with all relations?

推荐答案

在Hibernate中,这实际上是一个非常讨厌的问题,实际上通常是ORM.

This is a rather nasty problem in Hibernate and actually ORM in general.

发生的事情是,许多(提取)连接导致创建了相当大的笛卡尔积.也就是说,其他任何联接都会在结果中出现新的列和新的行,从而导致(相当大的)正方形"结果.

What happens is that the many (fetch) joins cause a rather large cartesian product to be created. I.e for ever other join new columns and new rows appear in the result, leading to a (fairly) large 'square' result.

Hibernate需要从该表中提取图形,但是它不足以将正确的列与正确的实体进行匹配.

Hibernate needs to distill a graph from this table, but it's not smart enough to match the right columns to the right entities.

例如

假设我们有结果

A B C
A B D

需要成为:

 A
 |
 B
 /\
C  D

休眠可以从主键和一些编码魔术中扣除,图形必须是什么,但是在实践中,它需要明确的帮助才能实现.

Hibernate could deduct from the primary keys and some encoding magic, what the graph must be, but in practice it needs explicit help to pull this off.

一种实现方法是在关系上指定特定于Hibernate的@IndexColumn或JPA标准@OrderColumn.

One way to do this is by specifying the Hibernate specific @IndexColumn or the JPA standard @OrderColumn on the relations.

例如

@Entity
public class Question {


    @ManyToMany
    @JoinTable(
        name = "question_to_answer",
        joinColumns = @JoinColumn(name = "question_id"),
        inverseJoinColumns = @JoinColumn(name = "answer_id")
    )
    @IndexColumn(name = "answer_order")
    private List<Answer> answers;

    // ...
}

在此示例中,我使用的是联接表,带有额外的列answer_order.通过此列(每个Question/Answer关系具有唯一的序号),Hibernate可以区分结果表中的条目并创建所需的对象图.

In this example I'm using a join table, with an extra column answer_order. Via this column, which has a unique sequential number per Question/Answer relation, Hibernate can distinguish the entries in the result table and create the required Object graph.

请注意,如果涉及多个实体,那么使用那么多急切的联接可能会导致结果集比您根据所涉及实体的数量所想的要大得多.

One note btw, if it concerns more than a few entities, using so many eager joins can potentially lead to a much larger result set than you might think based on the number of entities involved.

进一步阅读:

  • Hibernate Exception - Simultaneously Fetch Multiple Bags 1
  • Hibernate Exception - Simultaneously Fetch Multiple Bags 2
  • Solving simultaneously fetch multiple bags - Using the @IndexColumn

这篇关于Hibernate抛出MultipleBagFetchException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆