Hibernate – Getting "MultipleBagFetchException" when using OneToOne, but not when using OneToMany

huangapple 未分类评论52阅读模式
英文:

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         &#224;&#204;&#239;H   sRGB &#174;&#206;&#233;   gAMA  &#177;&#252;a   	pHYs  &#195;  &#195;&#199;o&#168;d   IDATWc&#188;&#236;&#255;Ÿ`‚&#210;€&#246; q=†K&#237;&#242;&#162;    IEND&#174;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?

huangapple
  • 本文由 发表于 2020年5月5日 09:04:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/61604120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定