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

问题描述 投票:11回答:2

我想检查一个集合中至少有一个元素(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
hibernate collections hql
2个回答
0
投票

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

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

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列表。

© www.soinside.com 2019 - 2024. All rights reserved.