GORM'标准'具有多个多对多关联

时间:2013-11-25 22:11:45

标签: grails gorm

假设您有三个域obj定义如下:

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
        }
    }
}

为什么getAuthors()会因ERROR spi.SqlExceptionHelper - Parameter "#1" is not set;失败,但如果使用Criteria重写,则可以正常工作:

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

我有'where query'的语法错误???

1 个答案:

答案 0 :(得分:2)

您的查询的标准似乎有点误导。 bookswords都是关联,您希望words等于word对象的单个实例。

你可以试试这个:

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