2011-08-23 82 views
4

我想知道,如果grails createCriteria支持多個屬性像純SQL一樣。我想列出這樣的條目:Grails createCriteria group by(groupProperty function)多個屬性

def criteria = DomainClass.createCriteria() 
def results = criteria.list { 
    groupProperty('parameterA') 
    groupProperty('parameterB') 
} 

這將列出剛剛與獨特的參數α和parameterB組合條目。問題是,這不起作用,有沒有解決方案,或者我應該使用hsql或類似的東西?

感謝, 刁

回答

2

你可以嘗試HQL。請記住,Criterias實際上是構建器,因此它們將始終只是HQL的一個子集,您可以在其中添加儘可能多的屬性。

6

試圖把預測,如:

def criteria = DomainClass.createCriteria() 
def results = criteria.list { 
    projections{ 
     groupProperty('parameterA') 
     groupProperty('parameterB') 
    } 
} 

我寫了一些測試

void test2xGroupProperty(){ 
    def pablo = new Artist(name: 'Pablo').save() 
    def salvador = new Artist(name: 'Salvador').save() 
    new Portrait(artist: pablo, name: "Les Demoiselles d'Avignon 1", value: 10.00).save() 
    new Portrait(artist: pablo, name: "Les Demoiselles d'Avignon 2", value: 10.00).save() 
    new Portrait(artist: pablo, name: "Les Demoiselles d'Avignon 3", value: 10.00).save() 
    new Portrait(artist: salvador, name: "The Persistence of Memory 1", value: 20.00).save() 
    new Portrait(artist: salvador, name: "The Persistence of Memory 2", value: 20.00).save() 
    new Portrait(artist: salvador, name: "The Persistence of Memory 3", value: 20.00).save() 
    def artistValue = Portrait.withCriteria{ 
     projections{ 
      groupProperty('value') 
      groupProperty('artist') 
     } 
    } 
    assert [[10.00, pablo], [20.00, salvador]] == artistValue 
} 

查看更多樣本的位置: https://github.com/fabiooshiro/plastic-criteria/blob/master/src/groovy/plastic/criteria/CriteriaDocTests.groovy