Grails GORM在Grails控制台中抛出错误

时间:2014-09-26 16:44:14

标签: grails gorm grails-domain-class

我正在尝试在Grails控制台中运行GORM方法(在运行命令grails console之后)。我正在使用 Grails 2.3.11 。这是我从控制台运行的代码:

import com.company.dualenroll.registration.DualEnrollStudent

DualEnrollStudent.list()

我希望这可以毫无问题地工作,但它会抛出异常。值得注意的是,DualEnrollStudent域类来自内联插件。这是堆栈跟踪:

java.lang.IllegalStateException: Method on class [com.company.dualenroll.registration.DualEnrollStudent] was used outside of a Grails application. If running in the context of a test using the mocking API or bootstrap Grails correctly.
    at com.company.dualenroll.registration.DualEnrollStudent.currentGormStaticApi(DualEnrollStudent.groovy)
    at com.company.dualenroll.registration.DualEnrollStudent.list(DualEnrollStudent.groovy)
    at com.company.dualenroll.registration.DualEnrollStudent$list.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
    at ConsoleScript0.run(ConsoleScript0:3)
    at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:257)
    at groovy.lang.GroovyShell.run(GroovyShell.java:481)
    at groovy.lang.GroovyShell.run(GroovyShell.java:163)
    at groovy.lang.GroovyShell$run$1.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)
    at groovy.ui.Console$_runScriptImpl_closure17.doCall(Console.groovy:954)
    at groovy.ui.Console$_runScriptImpl_closure17.doCall(Console.groovy)

1 个答案:

答案 0 :(得分:1)

确保从app文件夹中运行“grails console”。此外,您还需要在以下位置运行代码:

DualEnrollStudent.withTransaction { status -> // your code here }

否则你会得到一个异常,告诉你你没有初始化Hibernate会话。