HibernateException:当GORM查询移入另一个域类时,找不到当前线程的会话

joh*_*sam 5 grails grails-orm

在grails中,我有一个Domain类,可以在BootStap.groovy中查询

def xref = AppXref.find{user_nm == 'john'}
Run Code Online (Sandbox Code Playgroud)

但是,一旦我将代码移动到另一个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
Run Code Online (Sandbox Code Playgroud)

这是我在Config.groovy中的hibernate配置

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
}
Run Code Online (Sandbox Code Playgroud)

我将cache.use_query_cache更改为true.但它没有任何区别.

inj*_*eer 10

域类方法不是事务性的,因此您必须确保它们在TX上下文中调用:要么将它们放入服务中,要么使用 .withTransaction{}

  • 解决方案是什么? (2认同)
  • 抱歉投反对票,但对于答案或原始提问者评论,很难推断如何应用该解决方案。 (2认同)

bin*_*iam 9

添加@Transactional方法对我有用.