GORM无法正常工作。获取错误由以下原因引起:org.hibernate.HibernateException:找不到当前线程

时间:2015-12-01 14:07:33

标签: hibernate grails gorm

我的代码如下所示:

def get(Long id){
    return Client.findById(id);
}

调用此方法后,我收到类似

的错误
2015-12-01 19:00:25,529 Failure execution thread for 1448974681594
org.springframework.dao.DataAccessResourceFailureException: 
Could not obtain current Hibernate Session;
nested exception is org.hibernate.HibernateException: 
No Session found for current thread

我使用grails 2.5.1和Hibernate插件":hibernate4:4.3.8.1"

1 个答案:

答案 0 :(得分:1)

错误是描述性的。执行代码的线程(get(...))没有绑定到它的休眠会话。也许您在新线程,线程池等中异步执行它?

如果是,您可以使用以下方法修复:

def get(Long id){
    return Client.withSession {
        return Client.findById(id);
    }    
}

而是在异步流开始的顶层执行此操作。