我正试图通过(v.0.7.8),在Grails应用程序中使用Mongodb。我用com.google.code.morphia.annotations.Entity注释注释了一个域类(它不在grails/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注释。现在错误消失了,但是数据库仍然是空的?
edit2:
现在我明白了:
URI
/Survey/survey/index
Class
java.lang.NoSuchMethodException
Message
survey.Survey.<init>()要么这个插件被严重窃听,要么它不像快速启动的微型例子所显示的那样容易设置。再说一遍:我做错什么了?
发布于 2012-06-14 20:41:39
下面是关于如何使用插件http://jkuehn.github.com/gorm-mongodb/guide/2.%20Quickstart.html的用户指南
这里有一个示例应用程序( https://github.com/jkuehn/gorm-mongodb )
发布于 2012-09-24 10:56:35
以下是你可以做的事情:
首先:如果你像我一样来自戈姆MongoDB,试着检查一下你的DataSource.groovy。并将mongo {}替换为mongodb{},这样mongodb-就可以工作了。
第二:编辑类,然后执行以下操作:
import com.google.code.morphia.annotations.Entity
@Entity
class Question {
///your code...
}我注意到,除非您是从其他对象扩展的,否则没有必要添加@Entity(‘质询’)。
编辑:好了,在我看到您的错误消息和代码之后,我刚刚意识到,您在Controller方法之外调用了q.save()。
这是你的密码:
def index() {
def q = new Question()
}
q.save()请尝试:
def index() {
def q = new Question()
q.save()
}https://stackoverflow.com/questions/11040967
复制相似问题