2013-11-25 31 views
1

假設你有三個域OBJ文件定義爲這樣:GORM「其中標準」多很多-to-many關聯

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查詢'的語法錯誤嗎?

回答

2

看起來您的查詢的標準是有點誤導。 bookswords都是關聯關係,並且您期望words等於word對象的單個實例。

你可以試試這個:

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

我的印象是比較藏品的實體時,有一個隱含的傳播操作。感謝澄清。 – mdlandon