我是一個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