Search code examples
grailsgrails-orm

Grails get domain object from database based on field with type as another domain type


Say I want to get books by authors who have "john" in their name. How could I do something like:

Book.createCriteria().list(){
    like('author.name', '%john%')
}

Solution

  • Assuming that a Book is related to Author you should be able to do the following:

    Book.createCriteria().list() {
      author {
        like('name', '%john%')
      }
    }
    

    You can read more about Querying Associations in the documentation.