Search code examples
grailsgrails-ormgrails-2.0

gorm withCriteria and groping by domain class property


I have the following entities in my app :

class Statistic {

    int hour
    ...
    Widget widget

}

 class Widget {
    String name
    ...
}

And I also have StatisticObj that is just DTO of Statistics domain

    class StatisticObj {
        int hour
        ...
        String widgetName
    }

I want to make criteria which would count Statistic and group by widgetName (so in the end I have count of Statistic per each widngetName) and then convert result to list of StatisticObj. My Criteria looks like this:

def results = Statistic.withCriteria {

    groupProperty('widget.name', 'widgetName')
   ..... projections { count ...} 
   }
    resultTransformer(Transformers.aliasToBean(StatisticObj.class))
}

but in the end I get result which is not grouped.

What I'm doing wrong? Thanks


Solution

  •  .withCriteria {
            createAlias('widget', 'w')
            ...
            projections {
    
                groupProperty("w.name", "displayValue")
            }
          ....
         }
    

    works now!