2013-04-27 52 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