Spring Hibernate 事务管理

下文介绍的是嵌入Java代码式的编写,不利于Spring 配置代码分离的配置需求。而且作为事务的处理也不应该出现在数据库的逻辑代码之中。

在使用Hibernate HSQL语言对数据库进行操作时,当操作的语句不在事务有效块内,那么执行的数据库语言只是对内存中的持久对象进行更新,没有真正修改数据库。必须使用以下代码进行提交事务才能真正修改数据库。此处理为编程式的事务管理。

Session session  = sessionFactory.openSession();
Transaction trans = null;
try{
trans = session.beginTransaction();
User usr = (User) session.get(User.class,1);
user.setUsername("hello");
session.save(usr);
trans.commit();
} catch ( HibernateException hex ){
trans.callBack();
} finally{
session.close();
}

其中User class 的代码如下文:

@Entity
@Table("user")
public class User{
private String username;
@Column("username")
public String getUsername(){
return username;
}
public void setUsername( String username ){
this.username = username;
}
}

spring 配置文件为,下文给出非侵入式编程的方法, 此方法需要在配置文件做额外的事务管理配置,并且在实现代码的程度上可以不引入事务管理代码,事务的管理由Spring接管。

<?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:p
="http://www.springframework.org/schema/p"
xmlns:tx
="http://www.springframework.org/schema/tx"
xmlns:context
="http://www.springframework.org/schema/context"
xsi:schemaLocation
="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>

<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory">
</bean>
<bean id="sessionFactory"
class
="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:configLocation="classpath:hibernate.cfg.xml">
</bean>
<context:component-scan base-package="com.baidu.daoImp"/>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>

其中在daoImp中引入事务管理

package com.baidu.daoImp;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifi
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.baidu.model.User;


@Repository
@Qualifier("userDAO")
public class UserDAOImp{

private SessionFactory sessionFactory;
@Autowired
@Qualifier("sessionFactory")
public void setSessionFactory( SessionFactory sessionFactory ) {
this.sessionFactory = sessionFactory;
}
@Transactional(propagation=Propagation.REQUIRED)
public User updateUser( long userid, User user ){
Session session = sessionFactory.getCurrentSession();
User older = (User)session.get( User.class, userid )
older.setUsername( usr.getUsername() );
session.save( older );
}

}

转载于:https://www.cnblogs.com/papertigerv5/archive/2011/11/02/2232172.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值