Grails createCriteria group by(groupProperty function)的多个属性

时间:2011-08-23 12:22:21

标签: grails grails-domain-class

我想知道grails createCriteria是否支持多个属性组,例如纯sql。我想列出这样的条目:

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

这将仅列出具有唯一parameterA和parameterB组合的条目。问题是这不起作用,是否有任何解决方案或我应该使用hsql或simmilar?

谢谢, 刁

2 个答案:

答案 0 :(得分:7)

尝试投射,例如:

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

答案 1 :(得分:2)

您可以尝试使用HQL。请记住,Criterias实际上是构建器,因此它们将始终只是HQL的一个子集,您可以在其中添加任意数量的属性。