Intellij IDEA Groovy/Grails Console cannot execute Database scripts

Answered

I will try to reproduce the problem so everyone can understand...

 

First of all, I have a Person Domain.

package com.clinton

class Person {
String name
Integer age
Date lastVisit

static constraints = {
}
}

Now, I can easily open Grails console, using the terminal
in the IDE or my Ubuntu termianl.
So I get the default Grails terminal by typing
grails console. Console Opens. See picture below.


Now, I will like to use the Console provided by Intellij for Grails/Groovy to achieve same result.

It works for non-database scripts, but fails for scripts that need database connections.

I go to Tools>Groovy Console

And I get this...

>
import com.clinton.Person

def p = new Person(name: 'Fred', age: 15, lastVisit: new Date()).save()
println(Person.count)
    

Result: java.lang.IllegalStateException: Either class [com.clinton.Person] is not a domain class or GORM has not been initialized correctly or has already been shutdown. If you are unit testing your entities using the mocking APIs
    at org.grails.datastore.gorm.GormEnhancer.stateException(GormEnhancer.groovy:161)
    at org.grails.datastore.gorm.GormEnhancer.findInstanceApi(GormEnhancer.groovy:167)
    at org.grails.datastore.gorm.GormEnhancer.findInstanceApi(GormEnhancer.groovy)
    at org.grails.datastore.gorm.GormEntity$Trait$Helper.currentGormInstanceApi(GormEntity.groovy:1322)
    at org.grails.datastore.gorm.GormEntity$Trait$Helper.save(GormEntity.groovy:98)
    at org.grails.datastore.gorm.GormEntity$Trait$Helper$save.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at com.clinton.Person.save(Person.groovy)
    at com.clinton.Person.save(Person.groovy)
    at org.grails.datastore.gorm.GormEntity$save.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
    at ideaGroovyConsole.run(ideaGroovyConsole.groovy:4)
    at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)
    at groovy.lang.GroovyShell.run(GroovyShell.java:518)
    at groovy.lang.GroovyShell.run(GroovyShell.java:497)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSite.invoke(PogoMetaMethodSite.java:169)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.call(PogoMetaMethodSite.java:71)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
    at console.run(console.txt:25)
    at groovy.ui.GroovyMain.processReader(GroovyMain.java:632)
    at groovy.ui.GroovyMain.processFiles(GroovyMain.java:540)
    at groovy.ui.GroovyMain.run(GroovyMain.java:382)
    at groovy.ui.GroovyMain.process(GroovyMain.java:370)
    at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)
    at groovy.ui.GroovyMain.main(GroovyMain.java:109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)
    at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:49)

Is this a bug, or I am just ignorant of ho to do it with Intellij. Thank you
5 comments
Comment actions Permalink

Does it work outside of IDEA?

0
Comment actions Permalink

Yes, as the screenshot shows, it works with the default Groovy Console. But doesn't work with the Console provided by Intellij IDEA. I will want to use the Intellij one, for code completion and other features

0
Comment actions Permalink

The intellij console works perfect with scripts that do not have database interactions. But fails when you have to save or update database.

0
Comment actions Permalink

We don't have any suggestions at the moment. Please submit a reproducible test case with the bug report at https://youtrack.jetbrains.com/issues/IDEA and responsible developer will investigate it.

0
Comment actions Permalink

Ok working on that thanks.

0

Please sign in to leave a comment.