2012-06-20 55 views
2

我有这样HQL加入Grails中

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实例的关系。 我想这与那些与Thing,我有实例相关联的所有Foo实例相关的Bar所有实例。

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

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

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

回答

3
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)有效东西的ID。

+0

真棒,谢谢你啊!我想通了像我张贴后吧:)不幸的是,我现在与另一关系结构类似的问题。小心看一下? http://stackoverflow.com/questions/11113527/hql-joins-in-grails-part-deux – ubiquibacon