2016-03-22 42 views
3

我正在運行grails 3.1.4,並且無法創建允許將多個域對象綁定到其他幾個域對象的模式。作爲我想要做的一個例子:Grails域 - 多對多關係

我有三個類。書籍,作者和閱讀列表。

作者可能寫了很多書。書籍可以由幾位作者撰寫。此外,ReadingLists由幾本書組成,書可以成爲多個閱讀列表的一部分。我應該如何去做這個域名?

我試圖將它們設置爲如下:

class Author { 
    Long id 
    String firstName 
    String lastName 
    static hasMany = [books: Book] 
} 

class ReadingList { 
    Long id 
    String name 
    static hasMany = [books: Book] 
} 

class Book { 
    Long id 
    String title 
    Integer pageCount 
    static belongsTo = [author: Author, readingList: ReadingList] 
    static hasMany = [authors: Author, readingLists: ReadingList] 
} 

在編譯時,我得到這個錯誤:

No owner defined between domain classes 
[class firstapp.ReadingList] and [class firstapp.Book] in a many-to-many relationship. 

我試圖改變我屬於關聯值是完全一樣的作爲hasMany,地圖鍵是複數動詞,但似乎也不起作用。有關我如何完成此任何想法?

感謝

+0

你可以爲橋接第四類中的所有ID的?這將擁有自己的唯一ID,然後是三個表鍵中的每一個的列。你可以用ID的每個唯一組合來填充它。 –

回答

0

坦白說,我不明白其中的道理,但Book這個定義應該工作,

class Book { 
    Long id 
    String title 
    Integer pageCount 
    static belongsTo = [Author, ReadingList] 
    static hasMany = [authors: Author, readingLists: ReadingList] 
} 

但是,沒有反向引用