抛出了MultipleBagFetchException [英] MultipleBagFetchException thrown

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

问题描述

我希望在我的存储库层中有一个选项来加载实体,所以我尝试添加一个方法,该方法应该尽可能加载具有所有关系的问题实体,但会抛出MultipleBagFetchException。我怎样才能解决这个问题?

  @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),

我如何获得一个初始惰性加载的问题对象,以便用所有关系加载?

解决方案>



会发生什么是许多(取)连接导致一个相当大的笛卡尔要创建的产品。也就是说,对于其他连接新列和新行出现在结果中,导致(相当)大平方结果。



Hibernate需要从此提取图表表,但它不够聪明,无法匹配正确的实体列。



例如

假设我们有结果

  ABC 
ABD

需要变成:

  A 
|
B
/ \
CD

Hibernate可以从主键和一些编码魔术,图形必须是什么,但实际上它需要明确的帮助来解决这个问题。



一种方法是通过指定Hibernate特有的 @IndexColumn 或JPA标准 @OrderColumn 关系。



Eg

  @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>答案;

// ...
}

在这个例子中我正在使用一个连接表,并带有一个额外的列 answer_order 。通过这个列,每个问题/答案关系都有一个唯一的顺序编号,Hibernate可以区分结果表中的条目并创建所需的对象图。



,如果它涉及多个实体,那么使用如此多的渴望连接可能会导致比根据涉及的实体数量而想象的更大的结果集。



进一步阅读:




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?

解决方案

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 needs to distill a graph from this table, but it's not smart enough to match the right columns to the right entities.

E.g.

Suppose we have the result

A B C
A B D

Which needs to become:

 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.

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

E.g.

@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;

    // ...
}

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.

Further reading:

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

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