简体   繁体   中英

Grails GORM throwing an error in the Grails console

I am trying to run GORM methods in the Grails console (after running the command grails console ). I am using Grails 2.3.11 . This is the code I am running from the console:

import com.company.dualenroll.registration.DualEnrollStudent

DualEnrollStudent.list()

I would expect this to work with no problems, but it is throwing an exception. It is probably worth noting that the DualEnrollStudent domain class comes from an inline plugin. Here is the stack trace:

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)

Make sure you run "grails console" from your app folder. And also you need to run your code within:

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

Or else you'll get an exception telling you that you haven't got a Hibernate Session initialized.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM