0

I am studying with Spring Data JPA.

I have a problem dealing with JPQL. I god three entities.

public class Cheat implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "cheat_seq", length = 10)
    private Long cheatSeq;

    @OneToMany(mappedBy = "cheat", fetch=FetchType.LAZY)
    private List<CheatGoodVote> goodVote;

    @OneToMany(mappedBy = "cheat", fetch=FetchType.LAZY)
    private List<CheatBadVote> badVote;

    // skipped.
}

and others are the entities that is mapped to above entity.

public class CheatGoodVote {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="vote_seq", length=10)
    private Long voteSeq;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="cheat_fk", referencedColumnName="cheat_seq")
    public Cheat cheat;

    // ..skipped
}

Two entities are nearly the same each other.

public class CheatBadVote {    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="vote_seq", length=10)
    private Long voteSeq;

    @Column(name="ip_address", nullable=false)
    private String ipAddress;

    @Column(name="reg_date", nullable=false)
    private Date regDate;

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="cheat_fk", referencedColumnName="cheat_seq")
    public Cheat cheat;

}

And when I call JPQL "@Query("SELECT c FROM Cheat c JOIN FETCH c.goodVote JOIN FETCH c.badVote WHERE SIZE(c.goodVote) <= :voteCnt")",

It returns errors:

Caused by: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags: [com.aibiigae1221.skyQuiz.data.entity.Cheat.goodVote, com.aibiigae1221.skyQuiz.data.entity.Cheat.badVote]
    at org.hibernate.loader.BasicLoader.postInstantiate(BasicLoader.java:75) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.loader.hql.QueryLoader.<init>(QueryLoader.java:106) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:210) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:141) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:77) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:553) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:662) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]
    ... 103 common frames omitted

I want to get Cheat, CheatGoodVote, CheatBadVote entitie together. Is the error come from jpql problem? or my entity mapping is wrong?

  • Possible duplicate of [Hibernate cannot simultaneously fetch multiple bags](https://stackoverflow.com/questions/4334970/hibernate-cannot-simultaneously-fetch-multiple-bags) – Alien Oct 13 '18 at 16:39

1 Answers1

0

It seems to be framework related bug. Depends on your hibernate version.

Consider this issue, looks like yours LINK

Cartesian product

Be aware that join fetching both colections you will end up with cartesian product of both. For more information about problem itself and solutions go HERE

Emil Hotkowski
  • 2,233
  • 1
  • 13
  • 19