Searching the Owned Members of a Many-to-Many Relationship
Question
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")
Solution
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow