1

我正在按照Grails 2.4.4 文档制作 Map of Objects,但我的对象没有被持久化到 Mongo DB。因此,我决定使用文档提供的完全相同的示例进行完整性检查,但它也不起作用。

脚步:

grails 创建应用程序测试

接下来,我将 mongo 插件包含到我的 BuilConfig.groovy 中:

compile ":mongodb:3.0.3"
//runtime ":hibernate4:4.3.6.1"

然后,我配置了我的 DataSource.groovy:

environments {
    production {
        grails {
            mongo {
                replicaSet = [ "xxxxxxxx"]
                username = "xxxx"
                password= "xxxxxxxxxxxxxx"
                databaseName = "xxxxxx"
            }
        }
    }
    development {
        grails {
            mongo {
                replicaSet = [ "xxxxxxxx"]
                username = "xxxx"
                password= "xxxxxxxxxxxxxx"
                databaseName = "xxxxxx"
            }
        }
    }
    test {
        grails {
            mongo {
                replicaSet = [ "xxxxxxxx"]
                username = "xxxx"
                password= "xxxxxxxxxxxxxx"
                databaseName = "xxxxxx"
            }
        }
    }
}

这是我的书域类:

package test

class Book {

    String name
    Map authors
    static hasMany = [authors:Author]

    static constraints = {
    }
}

这是我的作者域类:

package test

class Author {

    String name
    static constraints = {
    }
}

使用 grails 控制台,我运行了以下脚本:

import test.*

def a = new Author(name:"Stephen King")

def book = new Book(name:"test")
book.authors = ["stephen":a]
book.save(failOnError:true)

然后,我查看了我的 Mongo DB,但找不到应该保留的地图。

{
    "_id": 1,
    "name": "test",
    "version": 0
}

如果您对正在发生的事情有任何线索,或者您是否知道Map在 Grails 中保留 a 的任何解决方法,我真的很感激。

谢谢

4

1 回答 1

1

干得好。将以下内容添加到您的Book域类:

static embedded = ["authors"]

再想一想,为什么要使用 aMap而不是Listfor authors

由于这是一个 MongoDB 数据库,它将保存Author.

于 2016-07-29T07:28:36.853 回答