【问题标题】:Grails where query associations查询关联的 Grails
【发布时间】:2015-02-18 23:38:48
【问题描述】:

我有两个域类:

class MyForm{
    ....
    static hasMany = [items:MyFormItem]
}

class MyFormItem{
    String type
    static belongsTo=[myForm:MyForm]
}

我想查询 MyForm 并查找所有存在一种类型的项目但没有另一种类型的项目。

所以,我想找到所有 MyForm,其中有一个类型为“某物”的 MyFormItem,但它不能也有一个类型为“其他”的 MyFormItem。

我正在尝试使用 where 子句,但这似乎只是抓住了一切:

MyForm.where{
    items{type=='something' && type!='other'}
}

有没有一种方法可以通过使用 GORM 的一个查询来做到这一点?

【问题讨论】:

    标签: hibernate grails grails-orm grails-2.0


    【解决方案1】:

    我认为你可以使用withCriteria 方法来做到这一点。

    def results = MyForm.withCriteria {
        items {
            eq('type', 'something')
        }
        items {
            ne('type', 'other')
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-04
      • 2023-04-02
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      • 2014-04-06
      相关资源
      最近更新 更多