Hibernate引发MultipleBagFetchException-无法同时获取多个包 [英] Hibernate throws MultipleBagFetchException - cannot simultaneously fetch multiple bags

查看:153
本文介绍了Hibernate引发MultipleBagFetchException-无法同时获取多个包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hibernate在创建SessionFactory时抛出此异常:

Hibernate throws this exception during SessionFactory creation:

org.hibernate.loader.MultipleBagFetchException:无法同时获取多个包

org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags

这是我的测试用例:

Parent.java

@Entity
public Parent {

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

 @OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
 // @IndexColumn(name="INDEX_COL") if I had this the problem solve but I retrieve more children than I have, one child is null.
 private List<Child> children;

}

Child.java

@Entity
public Child {

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

 @ManyToOne
 private Parent parent;

}

这个问题怎么样?我该怎么办?

How about this problem? What can I do?

编辑

好的,我的问题是我父母的另一个父"实体,我的真实行为是:

OK, the problem I have is that another "parent" entity is inside my parent, my real behavior is this:

Parent.java

@Entity
public Parent {

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

 @ManyToOne
 private AnotherParent anotherParent;

 @OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
 private List<Child> children;

}

AnotherParent.java

@Entity
public AnotherParent {

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

 @OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
 private List<AnotherChild> anotherChildren;

}

Hibernate不喜欢使用FetchType.EAGER的两个集合,但这似乎是一个错误,我没有做异常的事情...

Hibernate doesn't like two collections with FetchType.EAGER, but this seems to be a bug, I'm not doing unusual things...

ParentAnotherParent中删除FetchType.EAGER可以解决问题,但是我需要它,因此真正的解决方案是使用@LazyCollection(LazyCollectionOption.FALSE)而不是FetchType(感谢Bozho ).

Removing FetchType.EAGER from Parent or AnotherParent solves the problem, but I need it, so real solution is to use @LazyCollection(LazyCollectionOption.FALSE) instead of FetchType (thanks to Bozho for the solution).

推荐答案

我认为较新版本的hibernate(支持JPA 2.0)应该可以解决此问题.但是,否则,您可以通过以下方法来注释集合字段:

I think a newer version of hibernate (supporting JPA 2.0) should handle this. But otherwise you can work it around by annotating the collection fields with:

@LazyCollection(LazyCollectionOption.FALSE)

请记住从@*ToMany注释中删除fetchType属性.

Remember to remove the fetchType attribute from the @*ToMany annotation.

但是请注意,在大多数情况下,Set<Child>List<Child>更合适,因此,除非您确实需要List-否则请选择Set

But note that in most cases a Set<Child> is more appropriate than List<Child>, so unless you really need a List - go for Set

但是请注意,使用集,您不会消除底层的笛卡尔积,如

But remind that with using sets you won't eliminate the underlaying Cartesian Product as described by Vlad Mihalcea in his answer!

这篇关于Hibernate引发MultipleBagFetchException-无法同时获取多个包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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