Hibernate在创建SessionFactory时会抛出此异常:
org.hibernate.loader.MultipleBagFetchException:不能同时获取多个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;
}
这个问题怎么样?我该怎么办?
编辑
好的,我的问题是我的一个“父级”实体在我的父级内部,我的真正行为是这样的:
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
的两个集合,但这似乎是一个bug,我没有做什么异常的事情...
从Parent
或AnotherParent
中删除FetchType.EAGER
可以解决问题,但我需要它,因此真正的解决方案是使用@LazyCollection(LazyCollectionOption.FALSE)
而不是FetchType
(感谢Bozho提供的解决方案)。
select * from master; select * from child1 where master_id = :master_id; select * from child2 where master_id = :master_id
。 - nurettinfetchType
的List<child>
,则可能会出现类似的错误,这些List<child>
是多个List<child>
。 - Big Zed