1

在一个gorm域类中,我可以做

def q = {
  property1{
    eq('attr', 0)
  }
}

MyDomainClass.list(q)

如何在运行时修改闭包“q”(或创建一个包含闭包“q”具有的限制的新闭包),例如我可以添加另一个属性限制?


更多细节

实际上我的问题是如何在域类层次结构中创建组合标准。

class Parent{
  int pAttr

  static def getCriteria(){
    def dummyParentCriteria = {
      eq('pAttr', 0)
    }
  }
}

class Child extends Parent{
  int cAttr

  static def getCriteria(){
    def dummyChildCriteria = {
      // (1) 
      eq('cAttr', 0)
    }
  }
}

在“dummyChildCriteria”中,我想避免重复父母的限制。

我想以某种方式结合 Parent 的 getCriteria 的结果 (1)


具有命名查询继承的解决方案

class Parent{
  int pAttr
  static namedQueries = {
     parentCriteria{
       eq('pAttr', 0)
     }
  }
}

class Child extends Parent {
  int cAttr
  static namedQueries = {
     childCriteria{
       parentCriteria() 
       eq('cAttr', 0)
     }
  }
}

但是,如果有人知道最初问题的答案,那就太好了!

4

1 回答 1

2

从 Grails 2.0.x 开始,您可以使用具有多种用途的分离条件查询,包括允许您创建通用的可重用条件查询、执行子查询和执行批量更新/删除。

使用 Detached Criteria,您可以使用Where Queries进行查询组合。

def parentCriteria = {
    pAttr == 0
}

def childCriteria = {
    cAttr == 0
}

def parentQuery = Parent.where(parentCriteria)
def childQuery = Child.where(parentCriteria && childCriteria)
于 2012-05-02T22:18:22.323 回答