JPQL多次连接获取返回错误“无法同时获取多个包”

时间:2018-10-13 07:17:42

标签: hibernate jpa jpql

我正在使用Spring Data JPA学习。

我在处理JPQL时遇到问题。我相信三个实体。

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.
}

和其他是映射到上方实体的实体。

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
}

两个实体彼此几乎相同。

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;

}

当我调用JPQL “ @ Query(” SELECT c FROM Cheat c JOIN FETCH c.goodVote JOIN FETCH c.badVote WHERE SIZE(c.goodVote)<=:voteCnt“)” 时,

它返回错误:

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

我想一起获取 Cheat CheatGoodVote CheatBadVote 。 错误是来自jpql问题吗?还是我的实体映射错误?

1 个答案:

答案 0 :(得分:0)

这似乎是与框架相关的错误。取决于您的休眠版本。

考虑此问题,看起来像您的LINK

笛卡尔积

请注意,通过联接同时获取两个集合,您将最终得到两个cartesian product。有关问题本身和解决方案的更多信息,请访问HERE