Grails: How to use exists / notExists in createCriteria?

According to this jira ticket ( Support for .exists () subqueries, etc. in HibernateCriteriaBuilder) ), the criterion exists / notExists was added to the HibernateCriteriaBuilder. I am looking for help using this feature because I cannot find any documentation.

The 'exists' method seems to take a QueryableCriteria object as a parameter, but I am not familiar with this class and cannot find any useful documentation.

My simplified class structure:

class Foo { static hasMany = [fooBars: FooBar] } class FooBar { static belongsTo = [Foo, Bar] Foo foo Bar bar } class Bar { String division static hasMany = [fooBars: FooBar] } 

My goal is to find Foo that:
a) do not have an associated bar
or
b) the associated split Bar is in the specified list of lines (or not in the list of specified lines)

I am using sqlRestriction now and it works correctly, but I would prefer to use the HibernateCriteriaBuilder methods:

 def crit = Foo.createCriteria() def results = crit.list() { sqlRestriction(""" not exists ( select 1 from Foo_Bar fb inner join Bar b on Foo_Bar.bar_id = b.id where fb.foo_id = this_.id and b.DIVISION in ('bad', 'terrible') ) """) } 

My code uses "does not exist", but the example syntax "exists" will help in exactly the same way.

thanks

+6
source share

Source: https://habr.com/ru/post/988458/


All Articles