GORM中是否存在'不在'等价物?

fir*_*iel 8 grails grails-orm

这可以在createCriteria()中转换吗?

SELECT * FROM node WHERE (node.type = 'act' AND nid NOT IN (SELECT nid FROM snbr_act_community)) LIMIT 10
Run Code Online (Sandbox Code Playgroud)

我知道有一个'in'运算符,这是我到目前为止所拥有的:

def c = VolunteerOpportunity.createCriteria()
def matchingActs = c.list {
    node {
        eq('type', 'act')
    }
    maxResults(10)
}
Run Code Online (Sandbox Code Playgroud)

只是想看看这是否可行.否则,我猜这在HQL中是可能的吗?

fir*_*iel 19

感谢Sammyrulez的代码.从中得到了一个想法.测试了它,但没有用.我修好了,这是最后的工作代码:

def ids = [14400 as long, 14401 as long]

def c = VolunteerOpportunity.createCriteria()
def matchingActs = c.list {
    node {
        eq('type', 'act')
        not { 'in'(ids) }
    }
    maxResults(10)
}
Run Code Online (Sandbox Code Playgroud)

现在我知道如何使用'not'运算符.非常感谢!


Sam*_*lez 8

我自己没试过,但是看看Grails doc和hibernate api,你可以使用Hibernate Criteria API 1的Restrictions类中的静态方法在这个构建器映射上创建节点.所以像

 def c = VolunteerOpportunity.createCriteria()
def matchingActs = c.list {
    node {
        not(in('propertyName', ['val1','val2']))
    }
    maxResults(10)
}
Run Code Online (Sandbox Code Playgroud)

因为你使用not方法链接in方法(返回Criterion)(以Criterion作为参数并返回否定版本)

  • 谢谢你的示例代码.了解了如何使用'not'运算符. (3认同)