3

I have a domain class that I use for querying .

TourIndex{
     Long tourId
     String country
     String location
     int availability
     // ... more fields
}

We use a series if "dynamic" criteria builders for searching based on a series of configurations that basically results in performing:

def detachedCriteria = new DetachedCriteria(TourSearchIndex)

detachedCriteria = detachedCriteria.build { eq('country','AR') }
detachedCriteria = detachedCriteria.build { eq('location','salta') }

I want to reutilize this logic and get the available filters with their respective results in the form

[['location1', '3'], ['location2', '5']]

My wildest guess was:

detachedCriteria = detachedCriteria.build{
  projections{
    property('location')
    countDistinct('tourId')
  }
}

But this results in a very obvious error

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]

With createCriteria I have a way to get the count distinct, according to How to Group property in Order Clause using Grails Criteria

def criteria = TourSearchIndex.createCriteria()

def result = criteria.list {
  projections{
    groupProperty('location')
    countDistinct('id','idDistinct')
  }
  order('idDistinct','desc') 
}

But I want to use the DetachedCriteria that already uses the rest of the application, is there a workaround for this? What I think is missing is the "groupProperty" in the DetachedProjection in DetachedCriteria's inner class.

Thanks in advance.

Community
  • 1
  • 1

2 Answers2

1

For DetachedCriteria, the syntax is a bit different.

detachedCriteria = detachedCriteria.build{

}.projections{
    property('location')
    countDistinct('tourId')
}.list()
elixir
  • 1,394
  • 1
  • 11
  • 21
0

I am not sure but from seeing your criteria and detachedCriteria query as well the error, you can try this

detachedCriteria = detachedCriteria.build{
  projections{
    groupProperty('location')
    countDistinct('tourId')
  }
}
  • Thanks Suganthan but this throws me a MissingMethodException. groovy.lang.MissingMethodException: No signature of method: grails.gorm.DetachedCriteria.groupProperty() is applicable for argument types: (java.lang.String) values: [location] Possible solutions: getProperty(java.lang.String), geProperty(java.lang.String, java.lang.String), gtProperty(java.lang.String, java.lang.String), property(java.lang.String), hasProperty(java.lang.String) As I Could see in DetachedCriteria'n inner classs DetachedProjections. I'm thinking that I can raise a jira in grails project if no one can help me. – Trygve Korssjen May 06 '14 at 03:21