在grails中,我有一个Domain类,可以在BootStap.groovy中查询
def xref = AppXref.find{user_nm == 'john'}
但是,一旦我将代码移动到另一个 Domain 类的方法中,就会出现以下错误。
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 中的休眠配置
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。但这并没有什么区别。
最佳答案
添加 @Transactional
对我有用的方法。
关于grails - 休眠异常 : No Session found for current thread when GORM query moved into another domain class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27724465/