由于hibernate4已经完全可以实现事务了 与spring3.1中的hibernatedao,hibernateTemplete等有冲突,所以spring3.1里已经不提供hibernatedaosupport,hibernateTemplete了,只能用hibernate原始的方式用session
Session session = sessionFactory.openSession();
Session session = sessionFactory.getCurrentSession();
在basedao里可以用注入的sessionFactory获取session
Session session = sessionFactory.getCurrentSession();
注意 配置事务的时候必须将父类baseServiceImpl也配上,要不然会出现错误:No Session found for current thread 以前不需要的
SessionFactory.getCurrentSession()的后台实现是可拔插的。因此,引入了新的扩展接口 (org.hibernate.context.spi.CurrentSessionContext)和
新的配置参数(hibernate.current_session_context_class),以便对什么是“当前session”的范围和上下文(scope and context)的定义进行拔插。
它定义 了单一的方法,currentSession(),特定的实现用它来负责跟踪当前的上下文session。
首先我们看看org.hibernate.context.spi.CurrentSessionContext
这个接口仅有一个方法:
SessioncurrentSession()
throws HibernateException
Retrieve thecurrent session according to the scoping defined by this implementation.
currentSession()表示 根据当前CurrentSessionContext的实现及定义返回”当前的Session”
这个接口…Hibernate中有3个类实现了这个接口
All Known Implementing Classes:
JTASessionContext, ManagedSessionContext, ThreadLocalSessionContext
1: org.hibernate.context.internal.ThreadLocalSessionContext - 当前session通过当前执行的线程来跟踪和界定。
2: org.hibernate.context.internal.JTASessionContext- 当前session根据JTA来跟踪和界定。这和以前的仅支持JTA的方法是完全一样的。
3: org.hibernate.context.internal.ManagedSessionContext..
Spring为事务管理,也实现了此接口:
1: org.springframework.orm.hibernate4.SpringSessionContext– 当前Session根据Spring和事务管理器来跟踪和界定.
这几种实现都提供了“每数据库事务对应一个session”的编程模型,也称作每次请求一个session。Hibernate session的起始和终结由数据库事务的生存来控制。
hibernate.current_session_context_class 配置参数定义了应该采用哪个org.hibernate.context.spi.CurrentSessionContext实现。
一般而言,此参数的值指明了要使用的实 现类的全名,但那两个内置的实现可以使用简写,即"jta"和"thread"。
hibernate.current_session_context_class=thread
实质是:
hibernate.current_session_context_class= org.hibernate.context.internal.ThreadLocalSessionContext
同理:
hibernate.current_session_context_class=jta
实质是:
hibernate.current_session_context_class= org.hibernate.context.internal.JTASessionContext
而在Spring @Transactional声明式事务管理,”currentSession”的定义为: 当前被 Spring事务管理器 管理的Session,此时应配置:
hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
分享到:
相关推荐
struts2hibernate4spring3整合
整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6。 此外,还有:log4j、slf4j、junit4、ehcache等知识点。 项目...
hibernate与spring整合demohibernate与spring整合demo
最全的 Spring4+SpringMVC4+Hibernate4整合源码
Hibernate4Spring3整合,测试小例子。
SSH(struts2,Hibernate,Spring)整合及测试亲测可用,百分之白亲测可用。
spring4+springmvc4+hibernate4 整合,免费
Struts2+Hibernate+Spring整合开发深入剖析与范例应用03源码
Struts 2+Hibernate+Spring整合开发技术详解sample.pdf
SpringMVC+Hibernate+Spring整合实例源码下载,很完整的例子,需要的可以下载源码学习
struts2+hibernate+spring框架整合实列
希望此套框架整合对深入了解和学校Struts2+Spring4+Hibernate4三大框架整合技术有帮助
Struts2 Spring3 Hibernate4 框架完美整合
Struts2+Hibernate+Spring整合开发技术详解19章网上书店完整源码(内附数据库导出文件) 与书上最后一章内容完全对应 可以结合书上教程进行最后一章学习
本项目是自己搭建SSH框架,整合微信登录demo,供初学者参考借鉴
springmvc spring hibernate整合Demo 包含了CRUD 帮助初学者快速学习springmvc
这是一个整合Struts2+Hibernate+Spring项目所用的所有jar包,含oracle数据库驱动包及mysql数据库驱动包
内含struts配置文件,spring配置文件,log4j配置文件, 整合所需的struts,hibernate,spring jar包
本光盘是《整合Struts+Hibernate+Spring应用开发详解》一书的配书光盘,书中的代码按章存放, 即第二章所使用的代码放在codes文件夹的02文件夹下,依次类推。 本光盘根目录下有11个文件夹,其内容和含义说明如下: ...
Struts2+Hibernate+Spring整合教程