Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 491
Sebastian Flückiger Avatar asked Oct 16 '25 01:10

Sebastian Flückiger


2 Answers

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.

like image 93
Paweł Piecyk Avatar answered Oct 19 '25 02:10

Paweł Piecyk


The following findAllBy query should work:

Product.findAllBySubIsNotNull()

You could also use a where query:

Product.where { sub.isEmpty() == false }
like image 35
blacktide Avatar answered Oct 19 '25 03:10

blacktide



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!