Grails 嵌套hasOne实体的GORM查询条件

Grails 嵌套hasOne实体的GORM查询条件,grails,gorm,Grails,Gorm,我正在尝试通过子实体的属性创建GORM条件查询过滤。因此,有这样的实体: class PaymentEntry { static hasOne = [category: PaymentCategory] static constraints = { category(nullable: true) } // other stuff } class PaymentCategory { static hasMany = [payments: PaymentEnt

我正在尝试通过子实体的属性创建GORM条件查询过滤。因此,有这样的实体:

class PaymentEntry {

  static hasOne = [category: PaymentCategory]

  static constraints = {
    category(nullable: true)
  }

  // other stuff
}

class PaymentCategory {

  static hasMany = [payments: PaymentEntry]

  // other stuff  
}
现在我正在尝试选择PaymentEntries,带有特定的类别。我试着这样做:

def c = PaymentEntry.createCriteria()

def res = c {
  'in'("category", categories)
}
class PaymentEntry {

  static belongsTo = [category: PaymentCategory]

  static constraints = {
    category(nullable: true)
  }

  // other stuff
}

class PaymentCategory {

  static hasMany = [payments: PaymentEntry]

  // other stuff  
}
categories
这里是前面选择的
PaymentCategory
实体列表

不幸的是,这失败了。Grails抛出NoSuchMethodException

您应该在列表中输入。 试试这个:

def res = c {
  inList("category", categories)
}

有许多问题
hasOne
是指一对一,但实际上是一对多。因此,第一步是修复关联,可能如下所示:

def c = PaymentEntry.createCriteria()

def res = c {
  'in'("category", categories)
}
class PaymentEntry {

  static belongsTo = [category: PaymentCategory]

  static constraints = {
    category(nullable: true)
  }

  // other stuff
}

class PaymentCategory {

  static hasMany = [payments: PaymentEntry]

  // other stuff  
}
接下来,一旦有了criteria实例,就需要调用它的一个方法,例如
list()
,来构建和执行查询

def c = PaymentEntry.createCriteria()

def res = c.list {
  'in'("category", categories)
}  
同一事物的简短版本是

def res = PaymentEntry.withCriteria {
  'in'("category", categories)
}  

in()
inList()
都是可用的,只要您像您一样在
中引用
,因为它是Groovy关键字。您可以阅读更多有关条件查询的信息。

哪一个Grails版本?但我不希望PaymentCategory中有
付款
字段。我真的需要它吗?