2013-11-27 34 views
0

通過多個'hasMany'關聯執行請求的快速和最佳方式是什麼?通過多個hasMany關聯執行請求的快速方法

這裏有一個簡單的代碼,我有。

從A的實例我想要獲得B域(ID列表)的所有實例,其中login ==「toto」?

class A { 

    static hasMany = [BList:B] 

} 


class B { 

    static hasMany = [CList:C] 

} 


class C { 

    static hasMany = [DList:D] 

} 


class D { 

     String login 
} 

感謝

回答

1

您可以用HQL做到這一點:

String query = """ 
from a.bList 
where a = :a 
    and exists (select 1 
       from C c 
        , D d 
       where d.c = c 
       and c.b in a.bList 
       and d.login = :login) 
""" 
def result = A.executeQuery(query, [a: aInstance, login: "login"]) 

更多信息有關的executeQuery in the docs

+0

感謝您的回覆。當我執行這個查詢時,我有一個例外: 「無法解析屬性:一個:....」 – Jils

+0

@Jils你沒有B中的'belongsTo'? –

+0

不,我沒有它 – Jils

相關問題