Grails Mongodb嵌入式文档?Bug?

时间:2011-12-02 01:26:21

标签: grails mongodb gorm grails-plugin gorm-mongodb

我有这个简单的模型:

abstract class Info {
    ObjectId id
    Date dateCreated
    Date lastUpdated
}
class Question extends Info {
    String title
    String content
    List<Answer> answers = []
    static embedded = ['answers']
}
class Answer {
    String content
}

以这种方式撰写,回答嵌入在问题中(回复没有维护 ID )。我想为每个答案维护 id dateCreated lastUpdated 字段。所以我尝试以下方法:

class Answer extends Info {
    String content
}

当我运行一个简单的测试用例(用1个答案保存一个问题)时,我得到以下内容:

> db.question.find()
{ "_id" : ObjectId("4ed81d47e4b0777d795ce3c4"), "answers" : [ { "content" : "its very 
cool", "dateCreated" : null, "lastUpdated" : null,  "version" : null } ], "content" : 
"whats up with mongodb?", "dateCreated" : ISODate("2011-12-02T00:35:19.303Z"), 
"lastUpdated" : ISODate("2011-12-02T00:35:19.303Z"), "title" : "first question", 
"version" : 0 }

我注意到Grails不会自动维护字段 dateCreated lastUpdate 。此外,还添加了版本字段,但也有 null 值,但有趣的是没有创建 _id 字段(即使我在信息类中定义了 id

在第二种情况下,我试试这个:

class Answer {
    ObjectId id
    String content
}

我得到以下输出:

> db.question.find()
{ "_id" : ObjectId("4ed81c30e4b076cb80ec947d"), "answers" : [ { "content" : "its very 
cool" } ], "content" : "whats up with mongodb?", "dateCreated" : ISODate("2011-12-
02T00:30:40.233Z"), "lastUpdated" : ISODate("2011-12-02T00:30:40.233Z"), "title" : 
"first question", "version" : 0 }

这次,还没有为嵌入文档创建 id 。对这种情况的任何解释?为什么没有 id 属性,为什么 dateCreated lastUpdated 版本 ?这是打算以这种方式工作,还是一个bug?

谢谢,

1 个答案:

答案 0 :(得分:-1)

这可能是由于grails框架如何进行转换(GORM模块)。 您可以从grails论坛获得更快/更好的答案。 基本上似乎只有一些自动行为(填写日期和objectid)只对根对象而不是子对象进行。 您还可以基于morphia签出替代ORM: http://www.grails.org/plugin/mongodb-morphia