Grails shell 看不到域对象
我是一名 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
我的印象是 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
我做错了什么?
编辑:好的,我看到了有关使用全名以及使用 .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
这次我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我赞同伯特的建议,使用控制台而不是 shell。关于异常:
您可以尝试使用事务显式运行此代码吗:
I second Burt's advice to use the console instead of the shell. Regarding the exception:
Can you try explicitly running this code with a transaction:
您需要该包,因为有可能(但不是一个好主意)在不同的包中拥有两个具有相同名称的域类。
对于第二个会话,它应该是 foo.save(),而不是 foo.save。
我更喜欢控制台,它更容易使用。运行“grails console”,Swing 应用程序将启动。它与常规的 Groovy 控制台有点不同,因为它有一个隐式的“ctx”变量可用,它是 Spring 应用程序上下文。您可以通过“ctx.getBean('fooService')”使用它来访问服务和其他 Spring beans
You need the package since it's possible (but not a good idea) to have two domain classes with the same name in different packages.
For the 2nd session it should be foo.save(), not foo.save.
I prefer the console, it's a lot easier to work with. Run 'grails console' and the Swing app will start. It's a little different from the regular Groovy console in that it's got an implicit 'ctx' variable available which is the Spring application context. You can use that to access services and other Spring beans via "ctx.getBean('fooService')"
您必须
import com.test.TestObj
或通过new com.test.TestObj()
引用它,如您所示。请注意,“
save
”不是属性,而是 Grails 在运行时用来装饰域类的动态方法。you will have to
import com.test.TestObj
or reference it bynew com.test.TestObj()
as you have shown.Note that '
save
' is not a propery but a dynamic method that Grails decorates the domain class with at runtime.