Sebastian Flückiger
Sebastian Flückiger

Reputation: 5555

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?

Upvotes: 4

Views: 2991

Answers (3)

Sudhir N
Sudhir N

Reputation: 4096

You can use where query like this

Product.where {
   sub.size() > 0
}

'==' in where queries equivalent to sizeGt criteria

Upvotes: 0

blacktide
blacktide

Reputation: 12076

The following findAllBy query should work:

Product.findAllBySubIsNotNull()

You could also use a where query:

Product.where { sub.isEmpty() == false }

Upvotes: 1

Paweł Piecyk
Paweł Piecyk

Reputation: 2789

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.

Upvotes: 6

Related Questions