zoran119
zoran119

Reputation: 11307

Nested findAllBy*

I have 3 domain classes:

class Contract {

    String referenceNumber

    static belongsTo = [subCategory:SubCategory]
}

class SubCategory {

    String name

    static belongsTo = [category:Category]
    static hasMany = [contracts:Contract]
}

class Category {

    String name

    static hasMany = [subCategories:SubCategory]
}

I want to find all contracts which belong to the given category (so get all sub categories for the given category and then get all the contracts for all these sub categories). This is what I tried:

Contract.findAllBySubCategory(SubCategory.findAllByCategory(Category.get(1)))

but it keeps giving me an error:

groovy.lang.MissingMethodException: No signature of method: Contract.findAllBySubCategory() is applicable for argument types: (java.util.ArrayList) values: [[SubCat01, SubCat02, SubCat03]] Possible solutions: findAllBySubCategory(java.util.List)

Does anyone know what I'm doing wrong?

Upvotes: 1

Views: 783

Answers (2)

Akusete
Akusete

Reputation: 10794

The answer by JamesA is correct, just for the sake of completeness I would also like to say that this seems suited to a criteria query

Category category = Category.get(1)
List<Contracts> contracts = Contract.createCriteria.list {
  subCategory {
    eq('category', category)
  }
}

Upvotes: 3

James Allman
James Allman

Reputation: 41158

The findAll dynamic finder returns a list but expects a scalar parameter by default. Try using the InList comparator.

Contract.findAllBySubCategoryInList(SubCategory.findAllByCategory(Category.get(1)))

Upvotes: 2

Related Questions