gpt4 book ai didi

Grails createCriteria group by(groupProperty 函数)多个属性

转载 作者:行者123 更新时间:2023-12-04 23:07:47 24 4
gpt4 key购买 nike

我想知道 grails createCriteria 是否支持像纯 sql 那样按多个属性分组。我想列出这样的条目:

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

这将仅列出具有唯一参数 A 和参数 B 组合的条目。问题是这不起作用,是否有任何解决方案,或者我应该使用 hsql 或类似的东西吗?

谢谢,
马特奥

最佳答案

尝试进行预测,例如:

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

关于Grails createCriteria group by(groupProperty 函数)多个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7160962/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com