Search code examples
grailsgrails-orm

GORM 'where criteria' with multiple many-to-many associations


Assuming you have three domain objs defined as such:

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

class Book {
    String name
    static belongsTo = [author: Author]
    static hasMany = [words: Word]
}

class Word {
    String text

    Set<Author> getAuthors() {
        // This throws GenericJDBCException:
        Author.where {
            books.words == this
        }
    }
}

Why does getAuthors() fail with ERROR spi.SqlExceptionHelper - Parameter "#1" is not set; but works fine if rewritten using a Criteria:

public Set<Author> getAuthors() {
    // This works as expected:
    Author.withCriteria {
        books {
            words {
                eq('id', this.id)
            }
        }
    }
}

Do I have the syntax of the 'where query' wrong???


Solution

  • It seems like the criteria for your query is sort of misleading. books and words are both associations and you are expecting that words to be equal to single instance of the word object.

    You can try this:

    def getAuthors() {
        Author.where {
            books{
                words {
                    id == this.id
                }
            }
        }.list()
    }