0

我正在尝试通过 Mongodb Morphia Plugin (v.0.7.8) 将 Mongodb 与 Grails 应用程序一起使用。我用 com.google.code.morphia.annotations.Entity 注释注释了一个域类(不在grails-app/mongo 文件夹内):

import com.google.code.morphia.annotations.Entity

@Entity("Question")
class Question {
    Integer order
    String question
}

现在我正在尝试将一个新实体保存到控制器中的数据库中:

def index() {
    def q = new Question()
}
q.save()

但这会引发 HTTP 500 错误:

java.lang.IllegalStateException
Method on class [Question] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.

我究竟做错了什么?

编辑:

我将我的域类移动到 grails-app/mongo 并删除了 @Entity 注释。现在错误消失了,但是数据库还是空的?

编辑2:

现在我得到:

URI
/Survey/survey/index
Class
java.lang.NoSuchMethodException
Message
survey.Survey.<init>()

要么这个插件有严重的错误,要么它不像快速入门微示例所暗示的那样容易设置。再次:我做错了什么?

4

2 回答 2

0

这是有关如何使用该插件的用户指南http://jkuehn.github.com/gorm-mongodb/guide/2.%20Quickstart.html

此处提供示例应用程序https://github.com/jkuehn/gorm-mongodb

于 2012-06-14T20:41:39.423 回答
0

以下是您可以做的事情:

首先:如果您像我一样来自 GORM MongoDB,请尝试检查您的 DataSource.groovy。并将 mongo {} 替换为 mongodb{} 以便 mongodb-morphia 可以工作。

第二:编辑您的课程,然后执行以下操作:

import com.google.code.morphia.annotations.Entity
@Entity
class Question {
   ///your code...
}

我注意到除非您从其他对象扩展,否则没有必要添加 @Entity('Question') 。

编辑: 好的,在看到您的错误消息和代码后,我才意识到,您在 Controller 操作方法之外调用 q.save() 。

这是你的代码:

def index() {
    def q = new Question()
}
q.save()

请试试:

def index() {
    def q = new Question()
    q.save()
}
于 2012-09-24T10:56:35.720 回答