我有一个用于查询的域类。
TourIndex{
Long tourId
String country
String location
int availability
// ... more fields
}我们使用一系列的if“动态”标准构建器进行搜索,基于一系列的配置,这些配置基本上会导致执行以下操作:
def detachedCriteria = new DetachedCriteria(TourSearchIndex)
detachedCriteria = detachedCriteria.build { eq('country','AR') }
detachedCriteria = detachedCriteria.build { eq('location','salta') }我想重用这个逻辑,并在表单中获得可用的过滤器及其各自的结果
[['location1', '3'], ['location2', '5']]我最大胆的猜测是:
detachedCriteria = detachedCriteria.build{
projections{
property('location')
countDistinct('tourId')
}
}但这导致了一个非常明显的错误
Caused by: org.h2.jdbc.JdbcSQLException: Column "THIS_.LOCATION" must be in the GROUP BY list; SQL statement:
select this_.location as y0_, count(distinct this_.tour_id) as y1_ from tour_search_index this_ where this_.country=? [90016-173]根据How to Group property in Order Clause using Grails Criteria的说法,使用createCriteria,我有一种方法可以使计数不同
def criteria = TourSearchIndex.createCriteria()
def result = criteria.list {
projections{
groupProperty('location')
countDistinct('id','idDistinct')
}
order('idDistinct','desc')
}但是我想使用已经在使用应用程序其余部分的DetachedCriteria,有解决方法吗?我认为缺少的是DetachedCriteria内部类的DetachedProjection中的"groupProperty“。
提前谢谢。
发布于 2014-05-05 12:33:25
我不确定,但是从你的条件和detachedCriteria查询以及错误来看,你可以试试这个
detachedCriteria = detachedCriteria.build{
projections{
groupProperty('location')
countDistinct('tourId')
}
}发布于 2017-07-28 23:29:58
对于DetachedCriteria,语法略有不同。
detachedCriteria = detachedCriteria.build{
}.projections{
property('location')
countDistinct('tourId')
}.list()https://stackoverflow.com/questions/23461220
复制相似问题