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

发布于 2024-10-06 20:50:44 字数 709 浏览 4 评论 0原文

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

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

I want to check whether at least one element of a collection (u.organisations) is contained in another collection (? = 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 (?)

How can I express the EACH_ELEMENT_OF with HQL?

My last trial is:

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 (?)
)

But I get the exception:

IllegalArgumentException occurred calling getter of Organisation.id

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒儿 2024-10-13 20:50:44

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

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

I guess you need a subquery to express it in SQL, therefore subquery is also needed in HQL:

select u from SystemUser u 
where not exists (
    select 1 from UserGroup g where g.user = u and g not in (?)
)
嗫嚅 2024-10-13 20:50:44

这是 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 (?)

我假设 Organization 实体有一个 id 字段,您可以传入 id 列表。

This is the classical example from Hibernate documentation:

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

In your case, this would be:

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 (?)

I'm assuming the Organisation entity has an id field and you can pass in the list of ids.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文