Thursday 27 November 2008

Counting using Grails/Hibernate criteria

Just a quick gotcha for grails criterias when you want to count the results from a query where there is a join involved. If you use

count = criteria.count {
projections {
rowCount()
}
query()
}

You'll get a count for each of the joins, you might think that you could use

count = criteria.count {
projections {
countDistinct('id')
}
query()
}

But that still doesn't work - you have to use

count = criteria.get {
projections {
countDistinct('id')
}
query()
}

Hmmm

No comments: