2013-05-13 45 views
0

我想從三個領域中的Grails列表(像內任何正規的編程語言加入)的Grails/Groovy中/格姆執行內部聯接查詢功能

這裏有我的域

class Category{ 
    Integer id 
    String name 
} 
class Tag{ 
    Integer id 
    String name 
} 
class Content{ 
    Integer id 
    Category category 
    Tag tag 
    String text 
} 
//-------- 
def contentInstance = Content.findAllWhere(id:id.toInteger()) 

我希望Content.text,Category.name和Tag.name在列表中能夠在視圖中顯示 謝謝

回答

2

您可以使用標準查詢API來選擇自定義列。 你可以使用這樣的:

Content.withCriteria { 
projections { 
    property('text') 
    category { 
     property('name') 
    } 
    tag{ 
     property('name') 
    } 
    }  
} 

,或者你可以像創建別名:

Content.withCriteria { 
createAlias("category","categoryAlias") 
createAlias("tag","tagAlias") 
projections { 
    property('text') 
    property('categoryAlias.name') 
    property('tagAlias.name') 

} 
and{ 
    eq('category.id','categoryAlias.id') 
    eq('tag.id','tagAlias.id') 
    }  
} 

希望你有這個想法......

+0

感謝它真的幫助。 – iMiX 2013-05-14 22:58:31