Entity Project
具有集合属性contributors
映射为@OneToMany
与实体的关系User
@Entity
@Table( name = "projects" )
public class Project {
...
@OneToMany
@JoinTable(name = "project_contributors")
private List<User> contributors = new ArrayList<User>();
...
}
然后我需要检查是否contributors
已经有一个user
with id
contributorId
添加之前。我正在尝试使用 HQL 查询,但我显然很无能。
我正在尝试什么:
Query query = session.createQuery(
"select p.contributors from Project p where p.id = :pId and p.contributors.id = :cId"
);
query.setParameter("pId", projectId);
query.setParameter("cId", contributorId);
@SuppressWarnings("unchecked")
List<User> res = (List<User>) query.list();
但它给出了错误
illegal attempt to dereference collection [project0_.id.contributors] with element property reference [id]
有好心人愿意给我一点推动吗?
我做的另一个尝试是
"select p.contributors as c from Project p where p.id = :pId and c.id = :cId"
但什么也没有。
contributors
是一个集合。因此,它没有名为 id 的属性。
Id 是该 Collection 元素的一个属性。
您可以通过加入集合而不是取消引用来解决该问题:
SELECT p
FROM Project pj
JOIN pj.contributors p
WHERE pj.id = :pId
AND p.Id = :cId
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)