2016-07-28 72 views
1

我在追踪Grails 2.4.4 documentation to make a Map of Objects,但我的对象没有被保存到MongoDB。因此,我决定使用文档提供的完全相同的示例进行完整性检查,但它也无效。Grails 2.4.4对象映射示例不保存到MongoDB

步骤:

Grails的创造,应用测试

接下来,我包括蒙戈插件我BuilConfig.groovy:

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

然后,我配置了我的数据源。 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 = { 
    } 
} 

这里是我的作者域类:使用Grails的控制台

package test 

class Author { 

    String name 
    static constraints = { 
    } 
} 

,我跑到下面的脚本:

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 
} 

我真的很感激,如果你有什么事情的任何线索,或者如果您知道任何解决方法坚持Grails的一个Map

谢谢

回答

1

你在这里。以下添加到您的Book域类:

static embedded = ["authors"] 

在第二个想法,为什么您使用的Map代替Listauthors

由于这是一个MongoDB数据库,它将保存所有Author的字段。

+0

谢谢@Shashank。这只是我将以类似方式使用的文档提供的示例。嵌入式组件保存整个对象,但我需要的仅仅是对现有对象的“引用”。这将是类似以下内容: '书:{ \t “作家”: { \t \t “授予”:2131, \t \t “承认”:324324, \t \t “提到”:6575234, } }' – Steve