Grails: multiple relationships between two domain objects

前端 未结 3 863
孤城傲影
孤城傲影 2021-02-09 15:24

I am trying to implement two different types of relationships between two domain classes in Grails.

Consider the following; I have two domain classes, an Author and Book

3条回答
  •  情深已故
    2021-02-09 15:37

    Finally figured this out. Thanks to Don for pointing me in the direction of the db-reverse-engineer plugin which helped expose the key property that allows for this mapping strategy. Basically it all came down to using GORM's mappedBy association setting to explicitly tell Grails how the multiple hasMany references should be mapped. The class files that worked are as follows:

    class Author {
    
        String name
        static hasMany = [books: Book, favourites: Book]
    
        // need to disambiguate the multiple hasMany references with the 
        // 'mappedBy' property:
        static mappedBy =   [books: "author",
                            favourites: "authors"]
    }
    
    class Book {
    
        String title
        Author author
    
        static hasMany = [authors: Author]
        static belongsTo = [Author]
    }
    

    Thanks again for the help

提交回复
热议问题