本文介绍了HibernateException:当 GORM 查询移动到另一个域类时,没有找到当前线程的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在grails中,我有一个Domain类,可以在BootStap.groovy中查询
In grails, I have a Domain class and can be queried in BootStap.groovy
def xref = AppXref.find{user_nm == 'john'}
但是,一旦我将代码移动到另一个域类的方法中,就会出现以下错误.
However, once I moved the code into a method of another Domain class I will have the following error.
Servlet.service() for servlet [default] in context with path [/myapp] threw exception
Message: Could not obtain current Hibernate Session; nested exception is org.hibernate.HibernateException: No Session found for current thread
这是我在 Config.groovy 中的休眠配置
Here is my hibernate config in Config.groovy
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
// cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
singleSession = true // configure OSIV singleSession mode
flush.mode = 'manual' // OSIV session flush mode outside of transactional context
}
我将 cache.use_query_cache 更改为 true.但这没什么区别.
I changed cache.use_query_cache to true. But it made no difference.
推荐答案
域类方法不是事务性的,因此您必须确保它们是在 TX 上下文中调用的:要么将它们放在服务中,要么使用.withTransaction{}
domain class methods are NOT transactional, so you have to make sure, that they are invoked in a TX-context: either put them in a service, or use .withTransaction{}
这篇关于HibernateException:当 GORM 查询移动到另一个域类时,没有找到当前线程的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!