This is an extension of the question I asked here
I have a relationship like this
class Foo {
static hasMany = [bars: Bar]
}
class Bar {
// Has
Something like this should work:
select foo from Foo foo
where not exists (select thingBar.id from Thing thing left join thing.bars thingBar
where thing.id = :thingId
and thingBar.id not in (select fooBar.id from Foo foo2
left join foo2.bars fooBar
where foo2.id = foo.id))
EDIT: Now that you've explained what you wanted with a nice picture, it's simpler. In fact what you want is all the Foos which are linked to at least one bar (and not all the bars) also linked to the Thing. The query would thus be:
select foo from Foo foo
inner join foo.bars fooBar
where fooBar.id in (select thingBar.id from Thing thing inner join thing.bars thingBar)
Would something like this work ?
Foo.withCriteria {
bars {
in('id', thing.bars.collect { it.id })
}
}