Grails shell 看不到域对象

发布于 2024-08-17 22:40:30 字数 1051 浏览 4 评论 0原文

我是一名 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静若繁花 2024-08-24 22:40:30

我赞同伯特的建议,使用控制台而不是 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

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

import com.test.TestObj

TestObj.withTransaction{ status ->
    TestObj().save()
}

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()
}
只是一片海 2024-08-24 22:40:30

您需要该包,因为有可能(但不是一个好主意)在不同的包中拥有两个具有相同名称的域类。

对于第二个会话,它应该是 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')"

ゞ花落谁相伴 2024-08-24 22:40:30

您必须import com.test.TestObj或通过new com.test.TestObj()引用它,如您所示。

请注意,“save”不是属性,而是 Grails 在运行时用来装饰域类的动态方法。

groovy:000> foo = new com.test.TestObj();
===> com.test.TestObj : null
groovy:000> foo.save()
===> com.test.TestObj : 2
groovy:000> 

you will have to import com.test.TestObj or reference it by new 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.

groovy:000> foo = new com.test.TestObj();
===> com.test.TestObj : null
groovy:000> foo.save()
===> com.test.TestObj : 2
groovy:000> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文