我有两个桌子,男孩和女孩。假设男孩和女孩表之间存在多对多关系。在我的男孩实体中,我定义了以下集合:
@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
有任何想法吗?谢谢!!
最佳答案
如果您打算在此处将这种关系设为双向,请尝试添加maptedBy:
@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";
关于java - Hibernate无法提取结果集异常-多对多关系,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42794194/