2013-04-27 48 views
0

我遇到问题。如果可以,请你帮助我。从多个域中查询Grails

我称3个领域:

class DomainA { 
    long id 
    String propA1 
    String propA2 
    static hasMany = [domaniBs:DomainB] 
    } 

    class DomainB { 
    long id 
    String propB1 
    String propB2 
    static belongsTo = [forgeinKeyToDomainA:DomainA] 
    static hasMany = [domaniCs:DomainC] 
    } 

    class DomainC { 
    long id 
    String propC1 
    String propC2 
    static belongsTo = [forgeinKeyToDomainB:DomainB] 
    } 

想从DomainC得到所有记录中propA1 = “值1”

成像SQL代码:

Select DomainC.* 
from DomainA,DomainB,DomainC 
where DomainA.id=DomainB.forgeinKeyToDomainA 
and DomainB.id=DomainC.forgeinKeyToDomainB 
and DomainA.propA1="value1" 

可以执行SQL代码在Grails中,但希望使用Hibernate查询。

+0

你是什么意思 “可以在Grails的执行,并希望使用Hibernate的” 是什么意思?希望在HQL中使用?或者是别的什么? – acdcjunior 2013-04-27 03:03:02

+0

我在grails中安装了hibernate插件,并且我希望Hibernate方法可以完成这些工作。 DEF instantAlerts = Alert.where {频繁==(Frequently.findByID( “即时”))}。列表() instantAlerts.each {instantAlert-> \t \t \t DEF resultsInstantAlerts = KetQuaThongBaoEmail.where {sendStatus == 0 } .LIST() \t \t \t resultsInstantAlerts.each {result-> \t \t \t \t emailService.sendInstantNotification(result.alert.user.email,result.food) \t result.sendStatus = 1个//更新被发送 \t result.save(flush:true) \t} } – 2013-04-27 04:08:31

+0

我完成了我的任务,但我搜索其他方式(搜索更好的方式) – 2013-04-27 04:22:51

回答

0

就是这样。

HQL:

DomainC.executeQuery(""" 
Select c 
from DomainA a join a.domainB b join b.domainC c 
where a.propA1 = "value1" 
""") 

动态发现者

DomainC.findAllByDomainBInList(DomainB.findAllByDomainAInList(DomainA.findAllByPropA1("value1")) 
+0

哦。我会尝试。非常感谢。 – 2013-04-27 08:41:27

+0

我实现了上面的代码,但我无法检索想要的数据 – 2013-05-06 00:16:21

+0

Nguyen Le。我会说你必须添加样例输入和输出以及你的问题,以便我们知道你想要的数据。对于你的问题,上面的@James查询很有意义。另外,看看http://stackoverflow.com/questions/6707438/the-use-of-join-in-grails-gorm-queries – 2015-08-17 05:02:26