【问题标题】:Get the intersection of two sets in HQL在 HQL 中获取两个集合的交集
【发布时间】:2013-07-07 22:53:39
【问题描述】:

我在使用 HQL 时遇到了一些问题。

我想做一个相当复杂的查询,我有以下域对象(简化):

class SomeObject {
    Set<SomeOtherObject> otherObjects
}

我现在想要获取包含一个或多个指定的其他对象列表的所有对象。此外,我还想要一个黑名单,该对象不得包含其他对象。

我做到了这一点,我只能指定一个黑名单项和一个“允许的”其他对象列表:

select o from Object as o 
join o.otherObjects as otherObject 
where 
    otherObject in :allowedotherobjects 
    and :excludedotherobject not in elements(o.otherObjects)

基本上我想要这样的东西(这是不可能的):

select o from Object as o 
join o.otherObjects as otherObject 
where 
    otherObject in :allowedotherobjects 
    and elements(:excludedotherobjects) not in elements(o.otherObjects)

顺便说一句,这是我正在进行的一个 grails 项目,但我想在 HQL 中解决这个问题。

有什么想法吗?

【问题讨论】:

    标签: grails collections hql grails-orm


    【解决方案1】:

    试试这个:

    select o from Object as o 
    join o.otherObjects as otherObject 
    where 
        otherObject in :allowedotherobjects 
        and otherObject not in :excludedotherobjects
    

    【讨论】:

    • 另一种选择是从 SomeOtherObject 查询,这样会更容易。
    • 这似乎不起作用,就像“and otherObject not in :excludedotherobjects”部分没有做任何事情。您将如何从 otherObject 进行查询?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-29
    • 1970-01-01
    • 1970-01-01
    • 2016-01-20
    • 2010-12-16
    • 1970-01-01
    相关资源
    最近更新 更多