Grails shell没有看到域对象

Bra*_*ugh 6 grails groovy groovyshell

我是一个grails新手(和一个时髦的新手),我正在通过一些grails教程.作为一个新用户,grails shell对我来说是一个非常有用的小工具,但我无法弄清楚如何让它看到我的类和对象.这是我正在尝试的:

% 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
Run Code Online (Sandbox Code Playgroud)

我的印象是grails shell可以看到所有控制器,服务和域对象.怎么了?我需要在这里做点什么吗?

我尝试了另外一件事:

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
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

编辑:好的,我看到了关于使用全名和使用.save()而不是使用的答案.save.但是这个怎么样?

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
Run Code Online (Sandbox Code Playgroud)

这次我做错了什么?

Dón*_*nal 2

我赞同伯特的建议,使用控制台而不是 shell。关于例外情况:

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
Run Code Online (Sandbox Code Playgroud)

您可以尝试通过事务显式运行此代码吗:

import com.test.TestObj

TestObj.withTransaction{ status ->
    TestObj().save()
}
Run Code Online (Sandbox Code Playgroud)