@OneToMany annotation exception

Please help me find a solution to this problem.
There seems to be a problem in Hibernate with the @OneToMany annotation when there are multiple collections for fecth.
When I try to do this, it gives this exception Caused by: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags .
And this is my entity class:

 @Entity @Table(name = "game", catalog = "competition_manager") public class Game implements java.io.Serializable { private List<GamePlayerGoals> gamePlayerGoalses = new ArrayList<GamePlayerGoals>(0); private List<GamePlayer> gamePlayers = new ArrayList<GamePlayer>(0); @OneToMany(fetch = FetchType.EAGER, mappedBy = "game") public List<GamePlayerGoals> getGamePlayerGoalses() { return this.gamePlayerGoalses; } @OneToMany(fetch = FetchType.EAGER, mappedBy = "game") public List<GamePlayer> getGamePlayers() { return this.gamePlayers; } } 

But my question is: is it impossible to get more than the OnetoMany annotated collection in Hibernate? Thanks in advance.

+4
source share
1 answer

Hibernate has already told you what the problem is.

You cannot EAGER get more than 1 "many" collections at once. This is because the collection is combined in a request. Hibernate uses internally.

Combining more than one collection in this case will require querying the results of the β€œCartesian product,” fetching N * M strings is very inefficient when only N + M + 1 is required (N for players, M for goals, or something else).

Select one collection that you want EAGER and turn it off for another. And next time try reading the error message.

+2
source

Source: https://habr.com/ru/post/1491398/


All Articles