Hibernate 抛出的 MultipleBagFetchException [英] MultipleBagFetchException thrown by Hibernate

查看:8
本文介绍了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.

发生的情况是 many (fetch) 连接会导致创建一个相当大的笛卡尔积.即永远其他加入新列和新行出现在结果中,导致(相当)大的正方形"结果.

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 可以从主键和一些编码魔法中推断出图必须是什么,但实际上它需要明确的帮助来实现这一点.

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.通过这个列,每个问题/答案关系都有一个唯一的序列号,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 抛出的 MultipleBagFetchException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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