(Quick Reference)

findBy*

Purpose

Dynamic method that uses the properties of the domain class to allow the creation of Grails query method expressions that return one instance of the domain class. Returns only the first result, if any.

Examples

Given the domain class Book:

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

More examples:

def book = Book.findByTitle("The Shining")
book = Book.findByTitleAndAuthor("The Sum of All Fears", "Tom Clancy")
book = Book.findByReleaseDateBetween(firstDate, new Date())
book = Book.findByReleaseDateGreaterThanEquals(firstDate)
book = Book.findByTitleNotEqual("Harry Potter")
book = Book.findByReleaseDateIsNull()
book = Book.findByReleaseDateIsNotNull()

Description

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

  • LessThan
  • LessThanEquals
  • GreaterThan
  • GreaterThanEquals
  • Between
  • IsNotNull
  • IsNull
  • Equal
  • NotEqual
  • And

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.