我一个Grails新手(以及常规新手),并且我通过一些Grails的教程工作。作为一个新的用户,Grails的外壳对我来说是非常有用的小工具,但我无法弄清楚如何使它看到我的类和对象。这里就是我想:

% 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的外壳可以看到所有的控制器,服务和域对象。如何处理该事?我需要人在这里做什么?

我尝试另一件事情:

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

什么了我做错了?

有帮助吗?

解决方案

我第二Burt的建议使用控制台而不是外壳。有关异常:

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()
}

其他提示

您需要的软件包,因为它是可能的(但不是个好主意),有两个领域类在不同的包相同的名称。

有关的第二会话应该foo.save(),不foo.save。

我喜欢控制台,它是一个更容易的工作。运行“Grails的控制台”和Swing应用程序将启动。在它有一个隐含的“CTX”变量可用这是Spring应用程序上下文这是一个从普通的Groovy控制台有点不同。您可以使用通过“ctx.getBean(‘FooService接口’)”

访问服务和其他的Spring bean

,你将不得不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> 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top