Hibernate 在 SessionFactory 创建期间抛出此异常:
org.hibernate.loader.MultipleBagFetchException:无法同时获取多个包
这是我的测试用例:
父类.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;
}
孩子.java
@Entity
public Child {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne
private Parent parent;
}
这个问题怎么办?我能做些什么?
EDIT
好的,我遇到的问题是另一个“父”实体位于我的父实体内部,我的真实行为是这样的:
父类.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
,但这似乎是一个错误,我没有做不寻常的事情......
去除FetchType.EAGER
from Parent
or AnotherParent
解决了问题,但我需要它,所以真正的解决方案是使用@LazyCollection(LazyCollectionOption.FALSE)
代替FetchType
(谢谢Bozho为解决方案)。