Hibernate无法提取结果集异常 - 多对多关系

时间:2017-03-14 18:49:27

标签: java hibernate

我有两张桌子,男孩和女孩。让我们说男孩和女孩的表有很多关系。在我的男孩实体中,我定义了以下Set:

@ManyToMany(fetch = FetchType.LAZY)
public Set<Girl> getGirls() {
     return girls;
}

现在在Girl实体中我定义了以下Set:

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "boy_girl", joinColumns = {@JoinColumn(name = "girl_id")}, inverseJoinColumns = {@JoinColumn(name = "boy_id")})
public Set<Boy> getBoys() {
    return boys;
}

我正试图让男孩们挽回这些女孩。换句话说,我想要一个特定boyId的女孩名单。这是我正在使用的hql,我得到了前面提到的错误:

String query = "FROM Girl g inner join Boy b where b.boyId = :boyId";

Session session = getSessionFactory().getCurrentSession();
Set<Girl> girlSet = new HashSet<Girl>();
girlSet = (Set<Interaction>) session.createQuery(query).setParameter("boyId", boyId).getResultList();

这是确切的错误:

HTTP Status 500 - Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

如果您计划将此关系设置为双向,请尝试添加mappedBy:

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "boys")
public Set<Girl> getGirls() {
     return girls;
}

我还建议不要将@ManyToMany标记为渴望......但是请致电。

最后将查询更改为:

String query = "FROM Girl g inner join g.boys b where b.boyId = :boyId";