我有两张 table ,男孩和女孩。假设 boy 和 Girl 表具有多对多关系。在我的男孩实体中,我定义了以下集合:
@ManyToMany(fetch = FetchType.LAZY)
public Set<Girl> getGirls() {
return girls;
}
现在,在 Girl 实体中,我定义了以下集合:
@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
有什么想法吗?谢谢!!
最佳答案
如果您计划在此处将此关系设置为双向,请尝试添加mappedBy:
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "boys")
public Set<Girl> getGirls() {
return girls;
}
我还建议不要将 @ManyToMany 标记为 eager.. 但这是你的决定。
最后将查询更改为:
String query = "FROM Girl g inner join g.boys b where b.boyId = :boyId";
关于java - Hibernate 无法提取结果集异常 - 多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42794194/