Search code examples
hibernategrailsgrails-orm

Grails GORMS find all where collection property is not empty


i am fighting with gorms a bit:

String data = new JSON(Object.executeQuery("from Product p where p.subis not empty"))

is working just fine

however:

String data = new JSON(Product.findAllBySubIsNotEmpty())

does not work. the error

No signature of method: com.path.Object.findAllBySubIsNotEmpty() is applicable for argument types: () values: []

for the purpose of clean code i would prefer gorms syntax to hql queries, any ideas why this wont work?


Solution

  • It seems that you can't query for objects which has not-empty collection with dynamic finders (findAllBy*). You can do it using withCriteria instead:

    Product.withCriteria {
        isNotEmpty("sub")
    }
    

    It uses Hibernate's Criteria API, which is a bit more powerful than dynamic finders. Grails documentation is quite comprehensive about it.