实体Project具有contributors与@OneToMany实体关系映射的集合属性User
@Entity
@Table( name = "projects" )
public class Project {
...
@OneToMany
@JoinTable(name = "project_contributors")
private List<User> contributors = new ArrayList<User>();
...
}
Run Code Online (Sandbox Code Playgroud)
然后我需要检查,如果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();
Run Code Online (Sandbox Code Playgroud)
但它给出了错误
illegal attempt to dereference collection [project0_.id.contributors] with element property reference [id]
Run Code Online (Sandbox Code Playgroud)
有一个好的撒玛利亚人想给我一点推力吗?
我做的另一个尝试是
"select p.contributors as c from Project p where p.id = :pId and c.id = :cId"
Run Code Online (Sandbox Code Playgroud)
但没什么.
小智 16
contributors是一个集合.因此,它没有名为id的属性.
Id是此Collection的元素的属性.
您可以通过加入集合而不是取消引用它来解决问题:
SELECT p
FROM Project pj
JOIN pj.contributors p
WHERE pj.id = :pId
AND p.Id = :cId
Run Code Online (Sandbox Code Playgroud)
它有助于读取您的 HQL,就好像它是尝试取消引用 Java 类中的字段的 Java 代码一样:
p.contributors.id
Run Code Online (Sandbox Code Playgroud)
您正在尝试访问idof p.contributors,这是一个List<User>. 列表没有 ID。所以这是行不通的。
select p.contributors
Run Code Online (Sandbox Code Playgroud)
这样的查询,如果正确的话,将返回贡献者列表的列表。那也不是你想要的。
你会如何在 SQL 中做到这一点?通过加入。与 JPQL 相同:
select c from Project p
join p.contributors c
where p.id = :pId and c.id = :cId
Run Code Online (Sandbox Code Playgroud)
进行联接并为其分配别名允许查询关联的目标实体。
| 归档时间: |
|
| 查看次数: |
10300 次 |
| 最近记录: |