Grails criteria projections - get rows count

grails, hibernate, java

Solution

There are dynamic counters as well as finders on domain objects:

Hotel.countByCity(city)

More details in the user guide, of course

Problem

I have Hotel entity: ``` class Hotel { City city } ``` Now, i need count of hotels with given city. It could be done in this way: ``` def hotels = Hotel.findAllByCity(city) def cnt = hotels.size() ``` But it's very dirty way. It seems that with criteria it would be better, but i have no idea how to implement it...

Original source