Search code examples
grailsgrails-orm

GORM query criteria for nested hasOne entity


I'm trying to create GORM criteria query filtering by a property of sub-entity. So there are such entities:

class PaymentEntry {

  static hasOne = [category: PaymentCategory]

  static constraints = {
    category(nullable: true)
  }

  // other stuff
}

class PaymentCategory {

  static hasMany = [payments: PaymentEntry]

  // other stuff  
}

Now I'm trying to select PaymentEntries, with specific categories. I was trying something like this:

def c = PaymentEntry.createCriteria()

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

categories here is a list of PaymentCategory entities, selected earlier.

Unfortunately, this fails. Grails throws NoSuchMethodException.


Solution

  • You should have inList. Try this:

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