2010-08-06 57 views
7

这是可能的在createCriteria()中转换?GORM中是否存在“不在”等价物?

SELECT * FROM node WHERE (node.type = 'act' AND nid NOT IN (SELECT nid FROM snbr_act_community)) LIMIT 10 

我知道有一个“中”经营者和这里是我到目前为止有:

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

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

回答

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) 
} 

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

8

不是自己尝试过,而是看着Grails doc和hibernate api,在此构建器映射上使用Hibernate Criteria API的限制类1中的静态方法创建节点。因此,像

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

你既然链的方法(即返回一个标准),与没有方法(采用一个标准作为参数,并返回一个否定版本)

+3

感谢您的示例代码。了解如何使用'不'操作符。 – firnnauriel 2010-08-06 14:07:38

1

这是解决方案:

def resultat=EnteteImputationBudgetaire.createCriteria().get{ 
      between("dateDebutPeriode", dateDebut, dateFin) 

      and{ eq 'natureImputationBudgetaire','FONCTIONNEMENT' } 
      maxResults(1) 
     } 

     def resultat2=ParametragePlanBudgetaire.createCriteria().list() { 
      like('composantBudgetaire','6%') 
      if(resultat?.details) { 
       not { 
        'in'('composantBudgetaire',resultat?.details?.imputationBudgetaire) 
       } 
      } 
     } 
1

据Grails的文档有关创建标准here,你可以使用这样的事情:

not {'in'("age",[18..65])} 

在这个例子中,你有一个属性名为"age"和你想得到不在18到65之间的行。当然,[18..65]部分可以用你需要的任何值或范围列表代替。

1

只要记住:在这种情况下,你不必使用括号,你可以使用inList,例如:

not { inList 'age',[18..65] } 
相关问题