views:

113

answers:

1

With Book and Author domain classes as follows:

class Book { 
    static belongsTo = Author
    static hasMany = [authors:Author]
    String title
}

class Author {
   static hasMany = [books:Book]
   String name
}

How would I find the book by an author that has the title "Grails"?

I tried this but it didn't work (No signature of method: org.hibernate.collection.PersistentSet.findByTitle() is applicable for arguemnt types: (java.lang.String) value: [Grails].

Author author = Author.get(1)
def book = author.books.findByTitle("Grails")
+1  A: 

You can search by example as follows.

Author author = Author.get(1) def b = Book.find( new Book(title:'grails', author:author) )

see this link for info on how to do querys.

Jared
This is what I needed with a minor correction. I have an author instance, so the code becomes: Author author = Author.get(1) def b = Book.find( new Book(title:'grails', author:author) )Thanks!
byamabe