In Grails, I'm attempting to find an instance of a domain class that has exact entries in a one-to-many relationship. Consider this example:
class Author {
String name
List<Book> books
static hasMany = [books:Book]
}
class Book {
String title
static belongsTo = Author
}
My database then appears as such:
author book
------------------------- ------------------------
| id | name | | id | title |
|----|------------------| ------------------------
| 1 | John Steinbeck | | 1 | Grapes of Wrath |
| 2 | Michael Crichton | | 2 | East of Eden |
------------------------- | 3 | Timeline |
| 4 | Jurassic Park |
------------------------
author_book
----------------------------------------
| author_books_id | book_id | book_idx |
----------------------------------------
| 1 | 1 | 0 | // John Steinbeck - Grapes of Wrath
| 1 | 2 | 1 | // John Steinbeck - East of Eden
| 2 | 3 | 0 | // Michael Crichton - Timeline
| 2 | 4 | 1 | // Michael Crichton - Jurassic Park
----------------------------------------
What I'd like to be able to do is use a dynamic finder on author. I'm searching for an exact match on the hasMany relation, to match this behavior:
Author.findByBooks([1]) => null
Author.findByBooks([1, 2]) => author(id:1)
Author.findByBooks([1, 3]) => null
Author.findByBooks([3, 4]) => author(id:2)
Attempting this results in an ugly Hibernate error:
hibernate.util.JDBCExceptionReporter No value specified for parameter 1.
Has anyone had dynamic finders work with hasMany relationships of domain classes? What is the most 'Grails-y' solution to get the desired behavior?