One-to-many grails without table

I am new to Grails and GORM, and I am trying to implement a One-to-Many relationship. I tried the example in the document:

class Book {
    String title
}
class Author {
    static hasMany = [books: Book]
    String name 
}

Here are the generated tables:

AUTHOR
- Id (PK)
- Name

BOOK
- Id (PK)
- Title

AUTHOR_BOOK
- Author_Books_Id
- Book_Id

I was expecting something more:

AUTHOR
- Id (PK)
- Name

BOOK
- Author_Id (PK)
- Book_Index (PK)
- Title

Is there any way to achieve this (get rid of the connection table)?

+3
source share
2 answers

You must declare that the Book belongs to the Author. With attribTo, you declare that there is a foreign key in the Book table that references the Author identifier column. Like this:

class Book {
    String title
    static belongsTo = [author: Author]
}

class Author {
    static hasMany = [books: Book]
    String name 
}
+16
source

Just do it the other way around.

There is an author in the book.

class Book {
    String title
    Author author
}

class Author {
    String name 
}
0
source

All Articles