简体   繁体   English

具有Map和Mysql DB的Grails域类

[英]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, 任何人都可以解释一下Author Table的外观以及与Book的关系,

That's rather not possible, I would do a List of other objects (or hasMany relation), eg 那是不可能的,我会列出其他对象(或具有hasMany关系),例如

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 与Map.Entry相比,这种方法更加灵活和可扩展,在Map.Entry中您只能使用键和值,并且不能添加任何其他属性

Grails will create a table for you called author that will look like this: Grails将为您创建一个名为author的表,该表将如下所示:

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: 您还将获得另一个名为author_books表,该表包含您的books地图中的键/值对,如下所示:

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. 正如@Kamil Mikolajczyk所说,如果您使用Book对象的集合,则通常更易于理解模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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