Thursday, March 24, 2011

How to count in Grails/Hibernate: Message.countBy

How do I count the number of messages where my body length is between 0 and 25 characters long?

Message.countBy('from Message m where m.body.length <= 25')

Unfortunately for me, countBy does not take a string parameter.

From stackoverflow
  • Looking at the dynamic method reference, the best you can probably do is use executeQuery instead of count*:

    Message.executeQuery('select count(m) from Message m where SIZE(m.body) < 25')
    

    EDIT: Here are a couple of links that might help with writing/executing the query:

    : Thank you! You cannot do m.body.length i have discovered. You have to do SIZE(m.body) But very close
    Rob Hruska : I'll change it. I wasn't sure of the exact HQL, I had just copied what you were using, but with a different GORM method.

0 comments:

Post a Comment