3

I'm running with grails 3.1.4 and having trouble creating a schema that allows me to tie multiple domain objects to several other domain objects. As an example of what I'm trying to do:

I have three classes. Books, Authors, and ReadingLists.

Authors could have written many books. Books can be written by several authors. Also, ReadingLists are made up of several books and books can be part of multiple readingLists. How should I go about making the domain for this?

I've tried to set them up as follows:

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

While compiling, I get this error:

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

I've tried changing my belongsTo value to be the exact same as the hasMany, with the map keys being plural verbs, but that doesn't seem to work either. Any thoughts about how I can accomplish this?

Thanks

Tonyvolo
  • 31
  • 1
  • Could you have a fourth class for bridging all the ID's? This would have its own unique ID, and then a column for each of the three table keys. You would populate this with every unique combination of ID's. – Max xaM Mar 22 '16 at 04:24

1 Answers1

0

Frankly I don't understand the reason, but this definition of Book should work,

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

But no back references

aldrin
  • 4,482
  • 1
  • 33
  • 50