休眠抛出多个BagFetchException - 无法同时获取多个袋子
2022-08-31 04:17:04
休眠在 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
删除或解决问题,但我需要它,所以真正的解决方案是使用而不是(感谢Bozho的解决方案)。FetchType.EAGER
Parent
AnotherParent
@LazyCollection(LazyCollectionOption.FALSE)
FetchType