org.hibernate.HibernateException: No Session found for current thread的解决办法
来源:互联网 发布:黑魂3人物捏脸数据 编辑:程序博客网 时间:2024/06/05 13:24
在使用hibernate4集成spring4的时候遇到的错误org.hibernate.HibernateException: No Session found for current thread
在web.xml中加入一个过滤器
<pre name="code" class="html"><filter> <filter-name>openSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping>
原因大概是:
OpenSessionInViewFilter是Spring提供的一个针对Hibernate的一个支持类,其主要意思是在发起一个页面请求时打开Hibernate的Session,一直保持这个Session,直到这个请求结束,具体是通过一个Filter来实现的。
由于Hibernate引入了Lazy Load特性,使得脱离Hibernate的Session周期的对象如果再想通过getter方法取到其关联对象的值,Hibernate会抛出一个LazyLoad的Exception。所以为了解决这个问题,Spring引入了这个Filter,使得Hibernate的Session的生命周期变长。
而我们一般在Dao层通过getCurrentSession()来获取一个session,该方法从当前容器或是事务管理中获取一个会话,当时该方法并不保证在在当前容器中没有session会话的时候自动创建一个。如果不加这个过滤器,就会报上面的错误。
0 0
- org.hibernate.HibernateException: No Session found for current thread的解决办法
- org.hibernate.HibernateException: No Session found for current thread
- org.hibernate.HibernateException No Session found for current thread
- org.hibernate.HibernateException: No Session found for current thread
- org.hibernate.HibernateException: No Session found for current thread
- org.hibernate.HibernateException: No Session found for current thread
- org.hibernate.HibernateException: No Session found for current thread
- org.hibernate.HibernateException: No Session found for current thread
- org.hibernate.HibernateException: No Session found for current thread
- org.hibernate.HibernateException: No Session found for current thread
- org.hibernate.HibernateException: No Session found for current thread
- springMvc 中让人纠结的org.hibernate.HibernateException: No Session found for current thread
- 关于Hibernate中的update报org.hibernate.HibernateException: No Session found for current thread
- SpringMVC3+Hibernate4问题:org.hibernate.HibernateException: No Session found for current thread
- org.hibernate.HibernateException: No Session found for current thread异常处理
- 关于异常: org.hibernate.HibernateException: No Session found for current thread
- 在hibernate4中出现org.hibernate.HibernateException: No Session found for current thread
- Bug库____org.hibernate.HibernateException: No Session found for current thread
- iOS_多线程_GCD
- (2.1.16)java关键字assert、volatitle、instanceof、strictfp
- Windows Server 2008安装IIS7.0
- 递归与while循环实现求n!
- UI设计中的48dp定律
- org.hibernate.HibernateException: No Session found for current thread的解决办法
- UITableView总结
- 微软面试题系列
- 用Java实现简单的遗传算法
- android之测试
- 使用VisualVM进行
- Android 对Layout_weight属性完全解析以及使用ListView来实现表格
- HTML5 新增标签 及 不再使用标签
- Android获取网络上图片的