HQL joins in Grails: Part Deux

前端 未结 2 1319
野的像风
野的像风 2021-01-25 10:39

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          


        
相关标签:
2条回答
  • 2021-01-25 11:06

    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)
    
    0 讨论(0)
  • 2021-01-25 11:06

    Would something like this work ?

    Foo.withCriteria {
        bars {
            in('id', thing.bars.collect { it.id })
        }
    }
    
    0 讨论(0)
提交回复
热议问题