1

これは私がここで尋ねた質問の延長です

私はこのような関係を持っています

class Foo {
    static hasMany = [bars: Bar]
}

class Bar {
    // Has nothing to tie it back to Foo or Thing
}

class Thing {
    static hasMany = [bars: Bar]
}

のインスタンスがありますThing自分が持っているインスタンスに関連付けられているすべてのインスタンスに関連付けられているすべてのインスタンスを取得したいと思います。FooBarThing

私が望むことはHQLを介して可能ですか(HQLはとの間の間接的な関係をどういうわけか認識していThingますFooか)?

アップデート:

これが可能な関係の写真です。

ここに画像の説明を入力してください

私が持っていて、そのThing1すべてのインスタンスをFoo間接的に関連付けたい場合はBar、必要なソリューションが返さFoo1れ、Foo2

4

2 に答える 2

3

このようなものが機能するはずです:

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))

編集:あなたが素敵な写真であなたが望むものを説明したので、それはより簡単です。実際、必要なのは、Thingにもリンクされている少なくとも1つのバー(すべてのバーではない)にリンクされているすべてのFoosです。したがって、クエリは次のようになります。

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)
于 2012-06-20T06:01:13.943 に答える
0

このようなものは機能しますか?

Foo.withCriteria {
    bars {
        in('id', thing.bars.collect { it.id })
    }
}
于 2012-06-20T12:43:09.630 に答える