HQL:是另一个集合中一个集合的元素吗?

时间:2010-12-09 17:27:28

标签: hibernate collections hql

我想检查一个集合中至少有一个元素(u.organisations)是否包含在另一个集合中(? = excludedOrganisations):

select distinct u from SystemUser u
join u.userGroups g 
join u.organisations o
where 3 in elements(g.permissions) and
EACH_ELEMENT_OF(o) not in (?)

如何用HQL表达EACH_ELEMENT_OF

我的最后一次试验是:

select distinct u from SystemUser u 
join u.userGroups g 
where 3 in elements(g.permissions) and 
not exists (
    select org from Organisation org 
    where org in elements(u.organisations)
    and org not in (?)
)

但我得到了例外:

IllegalArgumentException occurred calling getter of Organisation.id

2 个答案:

答案 0 :(得分:0)

我猜你需要一个子查询来在SQL中表达它,因此在HQL中也需要子查询:

select u from SystemUser u 
where not exists (
    select 1 from UserGroup g where g.user = u and g not in (?)
)

答案 1 :(得分:0)

这是Hibernate文档中的经典示例:

from Cat as cat
left join cat.kittens as kitten
    with kitten.bodyWeight > 10.0

在您的情况下,这将是:

select distinct u from SystemUser u 
join u.userGroups g
join u.organisations o 
where 3 in elements(g.permissions) and 
 o.id not in (?)

我假设组织实体有一个id字段,你可以传入id列表。