hibernate4 spring3 整合
来源:互联网 发布:农村淘宝考试题和答案 编辑:程序博客网 时间:2024/05/17 23:29
原文链接:http://loushi135.iteye.com/blog/1420309
由于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 currentthread 以前不需要的
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。Hibernatesession的起始和终结由数据库事务的生存来控制。
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
- struts2+spring3+hibernate4整合
- hibernate4 spring3 整合
- hibernate4 spring3 整合
- spring3+Hibernate4+struts2整合
- Spring3整合Hibernate4
- spring3、hibernate4、JPA整合
- Spring3+hibernate4整合配置
- hibernate4 spring3 整合
- springmvc+hibernate4+spring3整合
- spring3整合hibernate4
- spring3+hibernate4+Activiti6整合
- hibernate4整合spring3事务问题
- spring3.1与hibernate4整合
- spring3.1与hibernate4整合
- spring3.1+struts2+hibernate4整合
- spring3与hibernate4的整合
- spring3+hibernate4整合(原创)
- Spring3与Hibernate4整合配置
- Linux2.6用户态API HooK手动操作
- C++ 三目运算符
- android平台上的json解析
- 《疯狂的号码》全网上线 揭“号码大战”内幕
- SIGPIPE信号处理
- hibernate4 spring3 整合
- 黑马程序员-Foundation的使用
- 随笔(2014.12)
- C++读取excel表格
- DDoS攻击防御方案
- 去掉UINavigationBar下面黑线方法
- Java NIO系列教程
- 【PB】PowerBuilder API Site
- Android开发学习 一