HQL加入Grails中(HQL joins in Grails)

2019-09-18 06:37发布

我有这样的关系

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

class Bar {
    // Has nothing to tie it back to Foo or anything else of importance
}

class Thing {
    // Has nothing to tie it back to Foo or anything else of importance
}

我有一个实例Thing我想获得的所有实例Bar是与所有实例相关联Foo了与实例相关联的Thing ,我有。

我使用的Grails executeQuery方法几次,但我的查询不起作用。

这是一个工作查询,将得到的所有实例Foo有关的实例Bar 。 我希望我所需要的查询将看起来非常相似,我只是有麻烦与HQL连接。

SELECT DISTINCT f FROM Foo f INNER JOIN f.bars bars WHERE bars =:bars

Answer 1:

SELECT DISTINCT f.Bars FROM Foo f inner join f.things thing where thing.Id in (:validThingIds)

Grails的支持应该设置参数,你基本上需要IDS的数组传递到查询..查询的最后部分应计算为(1,2,3,4)其中1,2,3,4是的IDS有效的东西。



文章来源: HQL joins in Grails