HQL加入Grails:部分Deux
这是我所问问题的延伸 我有这样的关系HQL加入Grails:部分Deux,grails,join,groovy,hql,Grails,Join,Groovy,Hql,这是我所问问题的延伸 我有这样的关系 class Foo { static hasMany = [bars: Bar] } class Bar { // Has nothing to tie it back to Foo or Thing } class Thing { static hasMany = [bars: Bar] } 我有一个东西的实例我想获取与我拥有的事物的实例相关联的Foo的所有实例。 我希望通过HQL实现什么(HQL是否意识到Thing和Foo之
class Foo {
static hasMany = [bars: Bar]
}
class Bar {
// Has nothing to tie it back to Foo or Thing
}
class Thing {
static hasMany = [bars: Bar]
}
我有一个东西的实例我想获取与我拥有的事物的实例相关联的Foo
的所有实例。
我希望通过HQL实现什么(HQL是否意识到Thing
和Foo
之间的间接关系)
更新:
这是一张可能的关系图
如果我有Thing1
,我想要所有通过Bar
与之间接关联的Foo
实例,那么我需要的解决方案将返回Foo1
和Foo2
,类似的东西应该可以工作:
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))
编辑:现在你已经用一张漂亮的图片解释了你想要什么,这就简单了。事实上,您想要的是所有链接到至少一个条(而不是所有条)的foo也链接到该对象。因此,问题是:
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)
你喜欢这个工作吗
Foo.withCriteria {
bars {
in('id', thing.bars.collect { it.id })
}
}
哇,这太糟糕了:)我能做些什么(一个以下的或其他关系定义)来减少查询的冗长程度吗?这是我能做的最好的了。也许存在一些更简单的问题,但我不知道是什么。你说的与所有bar实例关联是什么意思?@Baz1nga用图形支持更新了我的问题我更新了我的答案,因为你的问题实际上比最初的问题更简单。