HQL(Hibernate)如何检查元素列表是否是另一个列表的子集? [英] HQL (Hibernate) how to check if a list of elements is a subset of another list?

查看:117
本文介绍了HQL(Hibernate)如何检查元素列表是否是另一个列表的子集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难创建一个HQL查询来检查表'X'的记录'a'列表是否包含在同一个表'X'的另一个列表'b'中。



以下是我正在使用的示例:

 从角色r $中选择r 
b $ b其中(
从权限p
中选择p
,其中p.name ='test')元素(r.permissions)

现在这个查询只是一个例子,澄清了一下,每个角色都有一个或多个权限。



现在发生什么事情,如果子选择(权限p)返回1行,所有工作都正常。但如果选择了更多,查询失败,因为我试图检查列表是否在列表中......



我相信我在这里失去了一些东西,所以请任何帮助将被称赞。



谢谢



 从角色r中选择r不存在的地方(
select p.id从权限p
其中p.name ='test'
和p.id不在(从角色r2中选择p2.id内部连接r2.permissions p2
其中r2 = r ))

为了使它更清楚,这个查询检查是否没有名称'test'的权限不在角色权限中,这就像所说的名称为'test'的所有权限都在角色权限中一样。


I am having tough time creating a HQL query that checks if a list of records 'a' of table 'X' is included within another list 'b' of the same table 'X'.

Here is an example i am using:

select r 
from Role r 
where (
    select p 
    from Permission p 
    where p.name = 'test') in elements(r.permissions)

Now this query is just as an example, to clarify a bit, each role has 1 or more permission.

Now here's what happen, if the the subselect (permission p) returns 1 row, all work fine. But if more are selected, the query fails, since I am trying to check if a list is within a list...

I am sure I am missing something here, so please any help would be appretiated.

Thanks

解决方案

Maybe something simpler exists, but I've not found it:

select r from Role r where not exists (
    select p.id from Permission p 
    where p.name = 'test'
    and p.id not in (select p2.id from Role r2 inner join r2.permissions p2
                     where r2 = r))

To make it clearer, this query checks that there is no permission of name 'test' that is not in the role permissions, which is the same as saying that all permissions with name 'test' are in the role permissions.

这篇关于HQL(Hibernate)如何检查元素列表是否是另一个列表的子集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆