英文:
Hibernate - Getting "MultipleBagFetchException" when using OneToOne, but not when using OneToMany
问题
我有4个实体想要保存在我的数据库中:
@Entity
public class Zoo {
@Id
private Long id;
private String name;
// ...
}
@Entity
public class Animal {
@Id
private Long id;
private String name;
// ...
}
@Entity
public class EventLocation {
@Id
private Long id;
@OneToMany(mappedBy = "eventLocation")
private List<Section> sections;
// ...
}
@Entity
public class Section {
@Id
private Long id;
@ManyToOne
private EventLocation eventLocation;
@OneToMany(mappedBy = "section")
private List<Seat> seats;
// ...
}
@Entity
public class Seat {
@Id
private Long id;
// ...
}
然而,上面的代码不起作用。我收到以下错误:
org.hibernate.loader.MultipleBagFetchException: 无法同时提取多个 bag:[EventLocation.sections, Section.seats]
我发现奇怪的是,如果我将 "Zoo" 和 "Animals" 之间的 "OneToOne" 关系更改为 "OneToMany" 关系(这样,Zoo 包含一个 Animals 列表,而不仅仅是一个),那么上面的代码就能正常工作。
有人知道代码有什么问题吗?
英文:
I have 4 entities that I want to save in my database:
@Entity
‰PNG
IHDR àÌïH sRGB ®Îé gAMA ±üa pHYs à ÃÇo¨d IDATWc¼ìÿŸ`‚Ò€ö q=†Kíò¢ IEND®B`‚
}
However, the above code does not work. I get the following error:
org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags: [EventLocation.sections, Section.seats]
What I find weird about it is that if I change the "OneToOne" relationship between "Zoo" and "Animals" to a "OneToMany" relationship (so that Zoo contains a List of Animals instead of just one), then the above code works perfectly fine.
Does anyone have an idea what's wrong about the code?
专注分享java语言的经验与见解,让所有开发者获益!
评论