我是一个Grails新手(和一个groovy新手),我正在通过一些grails教程。作为一名新用户,grails shell对我来说是一个非常有用的小工具,但我无法弄清楚如何让它看到我的类和对象。这是我想要的: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
这次我做错了什么?
是的,withTransaction完美运作。我想知道为什么我需要补充一点。网上的例子似乎没有提到它。 – 2010-01-11 17:29:51
你不需要补充,但我想它可以解决你的问题。通过在一个事务中运行你的代码,你迫使创建一个hibernate会话(否则它会丢失)。 – 2010-01-11 23:15:57