【发布时间】:2014-02-21 00:54:26
【问题描述】:
Grails 版本:2.3.4
Hibernate 插件:运行时 ":hibernate:3.6.10.6"
Bootstrap.groovy 中产生错误的那一行:
def adminRole = new Role(authority: 'ROLE_USER').save(flush: true)
实际上任何类(控制器、Bootstrap.groovy)中的任何保存操作都会导致此错误。
但是当我得到在另一台计算机上创建的域类时,它工作正常,没有错误。
有什么建议吗?
谢谢。
完整的堆栈跟踪:
ERROR context.GrailsContextLoader Error initializing the application: Method on class [com.hib.Role] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.
java.lang.IllegalStateException: Method on class [com.hib.Role] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.
at BootStrap$_closure1.doCall(BootStrap.groovy:9)
at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:308)
at grails.util.Environment.executeForEnvironment(Environment.java:301)
at grails.util.Environment.executeForCurrentEnvironment(Environment.java:277)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
我没有使用 maven 或 mocking。
Botstrap.groovy
class BootStrap {
def init = { servletContext ->
def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true)
}
def destroy = {
}
}
【问题讨论】:
-
你能发布你的
Bootstrap.groovy吗?听起来您正在init闭包之外运行闭包 -
我添加了 Bootstrap.groovy。相关代码在 init 闭包内。
-
我认为它与 windows 8 有关。因为在 Mac OS X 和 windows 7 中没有错误。
标签: hibernate grails grails-orm