我是Grails的新手。
我正在尝试使用shell中的grails域进行体验,但我无法让它工作。当我运行应用程序时,这些域可以从脚手架代码中正常工作。
鉴于此域类
class IncomingCall {
String caller_id
Date call_time
int call_length
static constraints = {
}
}
我尝试创建一个“IncomingCall”并将其从shell中保存。无论我做什么,我总是得到“空虚”;该对象未创建。
如果我尝试创建对象然后进行保存,我会得到“没有绑定到线程的hibernate会话”错误(见下文)。
groovy:000> new IncomingCall(caller_id:'555-1212', call_time: new Date(), call_length:10).save()
ERROR org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionContext.currentSession (SpringSessionContext.java:63)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession (SessionFactoryImpl.java:574)
at groovysh_evaluate.run (groovysh_evaluate:3)
...
groovy:000>
如何从shell中完成这项工作?
答案 0 :(得分:0)
我发现从shell使用Grails域类通常不能很好地工作。我注意到的一件事是你没有任何import语句。如果你的类在com.my.domain包中,那么在尝试创建你需要的类的实例之前
import com.my.domain.*
答案 1 :(得分:0)
我也遇到了这个非常恼人的问题。
要修复它,请在shell中运行此代码以将hibernate会话绑定到事务同步管理器:
import org.hibernate.Session
import org.hibernate.SessionFactory
import org.springframework.orm.hibernate3.SessionFactoryUtils
import org.springframework.orm.hibernate3.SessionHolder
import org.springframework.transaction.support.TransactionSynchronizationManager
sessionFactory = ctx.getBean("sessionFactory")
session = SessionFactoryUtils.getSession(sessionFactory, true)
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session))
完成后,域对象应按预期工作。