我有一个 GrantedRole
类,它允许跨一个或多个 Site
授予特定角色,例如:
public class GrantedRole {
private RoleType role;
private User granter;
private User grantee;
private Collection<Site> grantSites;
@Column(nullable = false)
public UserRoleType getRole() {
return role;
}
@ManyToOne(optional = false, fetch=FetchType.LAZY)
public User getGranter() {
return granter;
}
@ManyToOne(optional = false, fetch=FetchType.LAZY)
public User getGrantee() {
return grantee;
}
@ManyToMany
public Collection<Site> getGrantSites() {
return grantSites;
}
}
我想要提出的是一个查询,它将找到特定授予者已给出的所有 GrantedRole
,这些角色具有一个或多个指定的 Site
> 在他们的 grantSites
集合中。所以类似:
SELECT g FROM GrantedRole g WHERE g.granter = :granter AND
g.grantSites [contains at least one of] (:sites)
...但我不确定 [包含至少一个]
使用什么语法,或者是否可以使用 JPA/HQL。有什么建议吗?
最佳答案
我能够通过调整此处找到的答案来完成这项工作:
Checking the Intersection of Two Collections via HQL
具体:
SELECT DISTINCT g FROM GrantedRole g, Site s WHERE g.granter = :granter
AND s IN (:sites) AND s IN ELEMENTS(g.grantSites)
ORDER BY g.grantee.firstName ASC, g.grantee.lastName ASC
关于java - JPA/HQL - 查找具有一对多关系且包含一个或多个特定元素的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14130953/