Grails GORM:绝妙的地图

发布于 2025-01-01 08:29:02 字数 689 浏览 1 评论 0原文

具有以下域类:

class Word {
    Map translations
    static hasMany = [translations: String]

    String toString(){
        id
    }
}

并且一些实例保存在 grails bootstrap 中:

def word1 = new Word(translations: [en:"game"]);
word1.save(failOnError: true, flush: true)

def word3 = new Word(translations: [en:"gate"]);
word3.save(failOnError: true, flush: true)

def word2 = new Word(translations: [en:"life"]);
word2.save(failOnError: true, flush: true)

无法正确获取 translations 字段。例如:

//input
println Word.findAll().each {
    println it.translations
}
//output
[:]
[:]
[:]
[1, 2, 3]

怎么了?

Having the following domain class:

class Word {
    Map translations
    static hasMany = [translations: String]

    String toString(){
        id
    }
}

And some instances saved in grails bootstrap:

def word1 = new Word(translations: [en:"game"]);
word1.save(failOnError: true, flush: true)

def word3 = new Word(translations: [en:"gate"]);
word3.save(failOnError: true, flush: true)

def word2 = new Word(translations: [en:"life"]);
word2.save(failOnError: true, flush: true)

It's not enabled to get translations field correctly. For example:

//input
println Word.findAll().each {
    println it.translations
}
//output
[:]
[:]
[:]
[1, 2, 3]

What's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青朷 2025-01-08 08:29:03

文档说:

如果你想要一个字符串/值对的简单映射,GORM 可以使用以下命令映射它:

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

The doc says:

If you want a simple map of string/value pairs GORM can map this with the following:

class Author {
    Map books // map of ISBN:book names
}
def a = new Author()
a.books = ["1590597583":"Grails Book"]
a.save()
风月客 2025-01-08 08:29:03

它应该

static hasMany = [translations: Map]

代替

static hasMany = [translations: String]

It should be

static hasMany = [translations: Map]

instead of

static hasMany = [translations: String]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文