简体   繁体   中英

Grails Domain Class with Map and Mysql DB

class Author {
    Map books // map of ISBN:book names
}
def a = new Author()
a.books = ["1590597583":"Grails Book"]
a.save()

Can anyone can explain, how Author Table will look and what relation to Book,

That's rather not possible, I would do a List of other objects (or hasMany relation), eg

class Book {
    String isbn
    String title
}

that's more flexible and extensible comparing to a Map.Entry where you are limited to key and value, and you can't add any other property

Grails will create a table for you called author that will look like this:

id | version
1  | 1
2  | 2
...

You will also get another table called author_books that contains key-value pairs from your books map and looks like this:

books    | books_idx | books_elt
authorId | someKey   | someValue

As @Kamil Mikolajczyk says though, your model will usually be easier to understand if you use a collection of Book objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM