Dynamic method that uses the properties of the domain class to allow the creation of Grails query method expressions that count the number of records returned


Given the domain class Book:

class Book {
   Long id
   Long version
   String title
   Date releaseDate
   String author

The following are all possible:

def c = Book.countByTitle("The Shining")
c = Book.countByTitleAndAuthor("The Sum of All Fears", "Tom Clancy")
c = Book.countByReleaseDateBetween(firstDate, new Date())
c = Book.countByReleaseDateGreaterThanEquals(firstDate)
c = Book.countByTitleLike("%Hobbit%")
c = Book.countByTitleNotEqual("Harry Potter")
c = Book.countByReleaseDateIsNull()
c = Book.countByReleaseDateIsNotNull()


GORM supports the notion of Dynamic Finders. The countBy* method counts the number of records for the given expression and returns the result.

The following operator names can be used within the respective dynamic methods:

The above operator names can be considered keywords, and you will run into problems when querying domain classes that have one of these names used as property names. For more information on dynamic finders refer to the user guide.