【问题标题】:Is there a 'not in' equivalent in GORM?GORM 中是否有“不在”等价物?
【发布时间】:2010-08-06 10:45:07
【问题描述】:

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

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

我知道有一个“in”运算符,这是我目前所拥有的:

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

只是想看看这是否可能。否则,我想这在 HQL 中是可能的吧?

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

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

    现在我知道如何使用“非”运算符了。非常感谢!

    【讨论】:

      【解决方案2】:

      我自己没有尝试过,但是查看 Grails 文档和休眠 api,您可以使用在 Hibernate Criteria API 1 的 Restrictions 类中找到的静态方法在此构建器映射上创建节点。所以像

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

      由于您将 in 方法(返回 Criterion)与 not 方法(将 Criterion 作为参数并返回否定版本)链接起来

      【讨论】:

      • 感谢示例代码。对如何使用“非”运算符有所了解。
      【解决方案3】:

      这就是解决方案:

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

      【讨论】:

        【解决方案4】:

        根据 Grails 关于创建标准here 的文档,您可以使用如下内容:

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

        在此示例中,您有一个名为 "age" 的属性,并且您想要获取不在 18 到 65 之间的行。当然,[18..65] 部分可以替换为您需要的任何值列表或范围。

        【讨论】:

          【解决方案5】:

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

          not { inList 'age',[18..65] }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-07-29
            • 2013-06-21
            • 2014-01-09
            • 2012-02-18
            • 2014-05-02
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多