Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
HQL加入Grails:部分Deux_Grails_Join_Groovy_Hql - Fatal编程技术网

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用图形支持更新了我的问题我更新了我的答案,因为你的问题实际上比最初的问题更简单。