Spring 整合Hibernate 事务代理类解决方案
2008-09-13 21:30
337 查看
myeclipse6.5创建java项目,添加spring+hibernate框架。
hibernate数据库操作时需要使用到事务,spring整合hibernate通过事务代理类的方式解决提交事务的问题,通过配置文件设置,Spring 配置文件 applicationContext.xml添加
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean
">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 声明一个 Hibernate 3 的 事务管理器供代理类自动管理事务用 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="StudentDAO" class="dao.StudentDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
</beans>
DAO 层核心类 dao.StudentDAO.java中增加标注,加入事务注释
/**
* Student 的 DAO 层,加入了事务标注。
*/
@Transactional
public class StudentDAO extends HibernateDaoSupport {
}
测试类
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentDAO dao = (StudentDAO)ctx.getBean("StudentDAO");
Student user = new Student();
user.setUsername("姓名");
user.setPassword("密码");
user.setAge(200);
dao.save(user);
hibernate数据库操作时需要使用到事务,spring整合hibernate通过事务代理类的方式解决提交事务的问题,通过配置文件设置,Spring 配置文件 applicationContext.xml添加
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean
">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 声明一个 Hibernate 3 的 事务管理器供代理类自动管理事务用 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="StudentDAO" class="dao.StudentDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
</beans>
DAO 层核心类 dao.StudentDAO.java中增加标注,加入事务注释
/**
* Student 的 DAO 层,加入了事务标注。
*/
@Transactional
public class StudentDAO extends HibernateDaoSupport {
}
测试类
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentDAO dao = (StudentDAO)ctx.getBean("StudentDAO");
Student user = new Student();
user.setUsername("姓名");
user.setPassword("密码");
user.setAge(200);
dao.save(user);
相关文章推荐
- Hibernate整合进spring---使用自己的事务处理
- spring与hibernate整合事务管理的理解
- Spring整合hibernate关于控制事务的问题
- spring hibernate整合时配置文件的事务部分(spring管理事务)
- spring下hibernate多数据库解决方案,以及跨库事务的尝试
- 学习SSH框架记录片4----Spring 声明式事务整合 Hibernate
- 【spring 6】Spring和Hibernate的整合:编程式事务
- Spring+Mybatis整合事务不起作用之解决方案汇总
- Spring声明式事务管理、Spring整合Hibernate
- 85-002-3 spring与hibernate的整合(采用AOP来管理事务实现声明式事务)
- 毕设—spring 和 hibernate在整合声明式事务的时候会报错
- Spring和Hibernate整合时对session及事务管理的理解
- Spring整合hibernate的事务属性介绍以及声明式事务管理 (使用Annotation和xml)
- hibernate4 spring4 spring代理事务配置 Error creating bea
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之五:Hibernate的事务管理、手动回滚
- spring-hibernate整合 事务不起作用
- 【spring 6】Spring和Hibernate的整合:编程式事务
- spring整合hibernate事务编程中错误分析
- spring hibernate整合事务管理器bean初始化失败
- 整合Struts+Spring+hibernate的时加载出错终极解决方案