休眠不能同时获取多个包 [英] Hibernate cannot simultaneously fetch multiple bags

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

问题描述

Hibernate在创建SessionFactory时抛出这个异常:


org.hibernate.loader.MultipleBagFetchException:不能同时获取多个包


这是我的测试用例:

Parent.java

  @Entity 
公共父母{

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

@OneToMany(mappedBy =parent,fetch = FetchType.EAGER)
// @IndexColumn(name =INDEX_COL)如果我有这个问题解决,但我检索更多的孩子比我有,一个孩子是空的。
私人列表< Child>儿童;

$ b

Child.java

  @Entity 
public Child {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
私人长ID;

@ManyToOne
私人父母;

}

这个问题怎么样?我能做什么?




编辑



好的,我遇到的问题是另一个父实体在我父母的内部,我真正的行为是这样的:
$ b

Parent.java

  @Entity 
公共父母{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private长ID;

@ManyToOne
私人AntoherParent anotherParent;

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

$ b $ / code>

AnotherParent.java

  @Entity 
public AntoherParent {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
私人长ID;

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

}

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



删除 FetchType.EAGER 来自 AnotherParent 解决了这个问题,但我需要所以真正的解决方案是使用 @LazyCollection(LazyCollectionOption.FALSE)而不是 FetchType (感谢 Bozho )。

我认为更新版本的hibernate(支持JPA 2.0)应该能够处理这个问题。但除此之外,您可以通过注释收集字段来处理它:

  @LazyCollection(LazyCollectionOption.FALSE)

记得从 fetchType 属性> @ * ToMany 注解。



但是请注意,在大多数情况下a Set< Child> List< Child> 更合适,所以除非你真的需要 List - 为 Set


Hibernate throws this exception during SessionFactory creation:

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

This is my test case:

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?


EDIT

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 AntoherParent anotherParent;

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

}

AnotherParent.java

@Entity
public AntoherParent {

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

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

}

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

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).

解决方案

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)

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

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

这篇关于休眠不能同时获取多个包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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