关于在Spring中Hibernate的No Session found for current thread问题
来源:互联网 发布:集贤一中网络 编辑:程序博客网 时间:2024/04/27 22:30
在我们的项目框架中,Hibernate配置Session方式有两种
一种是aop方式,如下图
一种是注解的方式,如下图
这两种方式的使用方式是有区别的,区别在于写Service实现类(即XxxServiceImpl)时,aop方式不需要任何的操作,只需要写上@Service注解即可,如下图:
而注解方式则需要在你所实现的方法上加上注解@Transactional即可,如下图:
在XxxServiceImpl使用注解的方式固然可行,但写程序时要注意效率,不可能在每个XxxServiceImpl下的每个方法写上@Transactional,因此,下面这种方法是优化的方法,即在XxxDaoImpl下写上@Transactional,如下图
注意这之间两图的差异:
图一 图二
在配置spring时,为什么要用这两种方式呢?主要是在Dao设计时,我们采用的是Hibernate的SessionFactory.getCurrentSession()方法。如下图:
如果不想在spring中配置,那么在Dao中的方法我们也将改,将采用Hibernate的SessionFactory.openSession()方法。如下图
采用openSession()后必须记住要手动关闭,而getCurrentSession()则是自动关闭的。
如果不采用以上几种方法,那么将会报异常“HTTP Status 500 - Request processing failed; nested exception is org.hibernate.HibernateException: No Session found for current thread”。
- 关于在Spring中Hibernate的No Session found for current thread问题
- Spring-Hibernate - No Session found for current thread
- Spring整合hibernate No Session found for current thread
- 在hibernate4中出现org.hibernate.HibernateException: No Session found for current thread
- 关于Hibernate中的update报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
- hibernate异常no session found for current thread
- org.hibernate.HibernateException: No Session found for current thread
- Hibernate No Session found for current thread异常分析
- spring+hibernate4 报错No Session found for current thread
- 关于在TreeSet中添加自定义类 报ClassCastException的解决方法
- hdu 1098 Ignatius's puzzle
- C# Excel控制
- 这辈子一定要对她好
- 当天晚上,我和母亲搂着满身是伤痕的弟弟
- 关于在Spring中Hibernate的No Session found for current thread问题
- 摄像头像素和帧数的骗局­
- python实现批量下载贴吧图片
- ORACLE 11G用于有效期
- mybatis实战教程(mybatis in action)之一:开发环境搭建
- 多个列表切换使用同一个listview,切换时回到各个列表之前的位置
- [leetcode] Interleaving String
- Composite 设计模式
- 纯JS实现的3D标签云,不依赖任何第三方库,支持移动页面