對Hibernate的編程事務管理

本文介绍了一个使用Spring框架和Hibernate实现的编程式事务管理案例。通过CustomersDAO类展示如何配置并应用编程式事务来确保数据库操作的原子性。该示例包括了Spring配置文件、DAO实现及简单的测试代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

對Hibernate的編程事務管理
2008年04月24日 星期四 15:44
對Hibernate的編程事務管理

------------------------------------------------------------------------------------------------------------------------
CustomersDAO.java
------------------------------------------------------------------------------------------------------------------------
package db;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

public class CustomersDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(CustomersDAO.class);

protected void initDao() {
// do nothing
}
private TransactionTemplate transactionTemplate;
public void setTransactionManager(
PlatformTransactionManager transactionManager) {
this.transactionTemplate = new TransactionTemplate(transactionManager);
}
public void insertUser(final Customers transientInstance) {
transactionTemplate
.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
public void doInTransactionWithoutResult(TransactionStatus status) {
getHibernateTemplate().saveOrUpdate(transientInstance);
}
});
}
}
------------------------------------------------------------------------------------------------------------------------

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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


<bean
>
<property
value="com.mysql.jdbc.Driver">
</property>
<property
value="jdbc:mysql://localhost:3306/sampledb">
</property>
<property value="root"></property>
</bean>
<bean
>
<property >
<ref bean="localMysql" />
</property>
<property >
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property >
<list>
<value>db/Orders.hbm.xml</value>
<value>db/Customers.hbm.xml</value>
</list>
</property>
</bean>



<bean
>
<property >
<ref bean="localSessionFactory" />
</property>
</bean>

<bean >
<property >
<ref bean="transactionManager" />
</property>
<property >
<ref bean="localSessionFactory" />
</property>
</bean>







</beans>
------------------------------------------------------------------------------------------------------------------------

Demo .java
------------------------------------------------------------------------------------------------------------------------

package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import db.Customers;
import db.CustomersDAO;
public class Demo {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
CustomersDAO dao = (CustomersDAO) ctx.getBean("userDAO");
Customers c = new Customers();
c.setId(1l);
c.setName("x");
dao.insertUser(c);
System.out.println("END");
}
}


------------------------------------------------------------------------------------------------------------------------



本篇日志被作者设置为禁止发表新评论


©2008 Baidu



引文来源 對Hibernate的編程事務管理_熊熊之家
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值