2010-12-09 121 views
10

我要檢查集合(u.organisations)的至少一個元素是否包含在另一個集合(? = excludedOrganisations):HQL:是另一個集合中的一個集合的元素嗎?

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

我怎樣才能表達EACH_ELEMENT_OF與HQL?

我最後的審判是:

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 
+0

嘿,你有沒有取得任何進展? – octav 2010-12-15 09:58:28

+0

我的解決方法是使用for-loop相對較少的元素有可能。 – deamon 2010-12-15 11:01:21

+0

嘗試通過,而不是組織列表,只有他們的ID列表。我試過類似的東西,它工作。 – octav 2010-12-16 09:01:04

回答

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

相關問題