Grails Expert Tip #8 – There’s a Bean for That

grails-expert-tipRecently I code reviewed some of the Grails (2.4.5) code of another team (of course :-)) and here’s a snippet I came across.

In some “search helper service”:

 public int countListWithoutFetching(Collection collection) {
   def session = grails.util.Holders.grailsApplication.mainContext.sessionFactory.currentSession
   return ((Integer) session.createFilter(collection, "select count(*)" ).list().get(0)).intValue()
 }

This team somehow needed some ‘generic’ way of counting the amount of records in the database, I thought.

Continue reading “Grails Expert Tip #8 – There’s a Bean for That”