HQL joins in Grails: Part Deux

拜拜、爱过 提交于 2019-12-02 07:53:34

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 })
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!