HQL加入Grails:Part Deux [英] HQL joins in Grails: Part Deux

查看:105
本文介绍了HQL加入Grails:Part Deux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我询问此处的问题的扩展



我有这样的关系

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

class Bar {
//没有任何东西可以与Foo或Thing相联系
}

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

我有一个 Thing 的实例。 我想获取与 Bar 的所有实例相关联的 Foo 的所有实例我有 Thing 的实例。



我想通过HQL(HQL)知道 Thing Foo )之间的间接关系?



更新:



以下是可能的关系图。



如果我有 Thing1 ,我希望所有通过 Bar 间接关联的 Foo 的实例c $ c>那么我需要的解决方案将返回 Foo1 Foo2

 从foo foo中选择foo应该是这样的:


不存在的地方(从Thing东西中选择thingBar.id连接变瘦g.bars thingBar
where thing.id =:thingId
和thingBar.id不在(从foo中选择fooBar.id foo2
left join foo2.bars fooBar
where foo2。编辑:现在你已经解释了你想要一个不错的东西图片,它更简单。实际上你想要的是所有的Foos都链接到至少一个栏(并不是所有的栏)也链接到Thing。因此,查询将是:

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


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 nothing to tie it back to Foo or Thing
}

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

I have an instance of Thing. I want to get all instances of Foo that are associated with all instances of Bar that are associated with the instance of Thing that I have.

Is what I want possible via HQL (is HQL somehow aware of the indirect relationship between Thing and Foo)?

UPDATE:

Here is a picture of a possible relationship.

If I had Thing1 and I wanted all the instance of Foo that are indirectly associated with it via Bar then the solution I need would return Foo1 and Foo2

解决方案

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)

这篇关于HQL加入Grails:Part Deux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆