Search code examples
grailsgroovygroovyshell

Grails shell not seeing domain objects


I'm a grails newbie (and a groovy newbie), and I'm working through some grails tutorials. As a new user, the grails shell is a really useful little tool for me, but I can't figure out how to make it see my classes and objects. Here's what I'm trying:

% grails create-app test
% cd test
% grails create-domain-class com.test.TestObj
% grails shell
groovy:000> new TestObj()
ERROR org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, groovysh_evaluate: 2: unable to resolve class TestObj

I was under the impression that the grails shell could see all of the controllers, services, and domain objects. What's up with this? Do I need to do something else here?

I tried one other thing:

groovy:000> foo = new com.test.TestObj();
===> com.test.TestObj : null
groovy:000> foo.save 
ERROR groovy.lang.MissingPropertyException: No such property: save for class: com.test.TestObj

What am I doing wrong?

EDIT: Okay, I saw the answers about using the full name and also using .save() instead of .save. But what about this one?

groovy:000> new com.test.TestObj().save()
ERROR org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

What'd I do wrong this time?


Solution

  • I second Burt's advice to use the console instead of the shell. Regarding the exception:

    groovy:000> new com.test.TestObj().save()
    ERROR org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    

    Can you try explicitly running this code with a transaction:

    import com.test.TestObj
    
    TestObj.withTransaction{ status ->
        TestObj().save()
    }