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;
}
这个问题怎么样?我能做些什么?
编辑
好的,我遇到的问题是另一个“父”实体在我的父级内部,我的真实行为是这样的:
父类.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;
}
另一个父母.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
,但我需要它,所以真正的解决方案是使用而不是(感谢Bozho的解决方案)。Parent
AnotherParent
@LazyCollection(LazyCollectionOption.FALSE)
FetchType