【发布时间】:2016-10-10 14:11:51
【问题描述】:
实体 Project 有一个集合属性 contributors 映射到实体 User 的 @OneToMany 关系
@Entity
@Table( name = "projects" )
public class Project {
...
@OneToMany
@JoinTable(name = "project_contributors")
private List<User> contributors = new ArrayList<User>();
...
}
然后我需要在添加之前检查contributors 是否已经有user 和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"
但什么都没有。
【问题讨论】: