Hibernate在创建SessionFactory时抛出此异常:
org.hibernate.loader.MultipleBagFetchException:无法同时获取多个包
这是我的测试用例:
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;
}
这个问题怎么样?我能做什么?
编辑
好的,我的问题是我父母的另一个“父”实体,我的真实行为是这样的:
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
,但这似乎是一个错误,我没有做异常的事情...
FetchType.EAGER
从问题中删除Parent
或AnotherParent
解决问题,但是我需要它,因此真正的解决方案是使用@LazyCollection(LazyCollectionOption.FALSE)
而不是FetchType
(感谢Bozho提供解决方案)。
select * from master; select * from child1 where master_id = :master_id; select * from child2 where master_id = :master_id
List<child>
与fetchType
用于定义一个以上 List<clield>