关于spring+hibernate的事务的简单配置(这里用的mysql数据库)
首先,我使用的mysql,设置mysql为InnoDB类型。
事务管理的控制应该放到业务逻辑层。你可以写个处理业务逻辑的JavaBean,在该JavaBean中调用DAO(或则是HibernateTemplate()的crud操作),然后把该Bean的方法纳入spring的事务管理。
方法一:xml文件定义如下:
//声明事务管理器
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
//声明事务代理,使用抽象属性(abstract="true") 方便所有的业务类都拥有这个事务代理
<bean id="baseTransactionProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>
<bean id="userManager" parent="baseTransactionProxy">
<property name="target" >
//具体的实现了业务逻辑的JavaBean
<bean class="org.zdpuyao.srm.service.imp.UserManageImp">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
</property>
</bean>
org.zdpuyao.srm.service.imp.UserManageImp就是我们的实现业务逻辑的JavaBean。我们通过parent元素声明其事务支持。
在测试的时候,就用
UserManage um=(UserManage) context.getBean("userManager");来取得bean对象(注意上边的"userManager"配置)
具体类
package org.zdpuyao.srm.service.imp;
public class UserManageImp extends HibernateDaoSupport implements UserManage {
public void addUser(Object obj) {
TblBsDepartmentType dept; //=(TblBsDepartmentType)temp.find("from TblBsDepartmentType").get(0);
dept = new TblBsDepartmentType();
dept.setBdtyName("java12");
getHibernateTemplate().save(dept);
///throw new ("ssss");
//throw new RuntimeSQLException("sssss");
/*TblBsUserType user = (TblBsUserType)obj;
user.setTblBsDepartmentType(dept);
temp.save(user);*/
}
}
具体测试类:
public class TestUser extends TestCase {
protected void setUp() throws Exception {
super.setUp();
}
private BeanFactory getFactory()
{
Resource res=new FileSystemResource("\\WebRoot\\WEB-INF\\applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(res);
return factory;
}
private UserManage getUserManage()
{
BeanFactory factory=getFactory();
UserManage um=(UserManage) factory.getBean("userManager");
return um;
}
public void testAddUser() throws Exception {
TblBsUserType user = new TblBsUserType();
user.setButyId(Long.valueOf(12345645));
user.setButyName("yangchuan");
user.setButyPassword("12345");
getUserManage().addUser(user);
//excuteJDBCTranscation();
}
}
///////////////////////////////////////////
方法二:xml文件定义如下:
1: 第一步:
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
1: 第二步:
<bean id="userManager"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="false">
<property name="transactionManager" ref="transactionManager" />
<property name="target">
<ref local="userManagerimp" />
</property>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>
1: 第三步:
<bean id="userManagerimp"
class="org.zdpuyao.srm.service.imp.UserManageImp">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
org.zdpuyao.srm.service.imp.UserManageImp就是我们的实现业务逻辑的JavaBean。
在测试的时候,就用
UserManage um=(UserManage) context.getBean("userManager");来取得bean对象
具体类
package org.zdpuyao.srm.service.imp;
public class UserManageImp extends HibernateDaoSupport implements UserManage {
public void addUser(Object obj) {
TblBsDepartmentType dept; //=(TblBsDepartmentType)temp.find("from TblBsDepartmentType").get(0);
dept = new TblBsDepartmentType();
dept.setBdtyName("java12");
getHibernateTemplate().save(dept);
///throw new ("ssss");
//throw new RuntimeSQLException("sssss");
/*TblBsUserType user = (TblBsUserType)obj;
user.setTblBsDepartmentType(dept);
temp.save(user);*/
}
}
具体测试类:
public class TestUser extends TestCase {
protected void setUp() throws Exception {
super.setUp();
}
private BeanFactory getFactory()
{
Resource res=new FileSystemResource("\\SRMnew\\WebRoot\\WEB-INF\\applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(res);
return factory;
}
private UserManage getUserManage()
{
BeanFactory factory=getFactory();
UserManage um=(UserManage) factory.getBean("userManager");
return um;
}
public void testAddUser() throws Exception {
TblBsUserType user = new TblBsUserType();
user.setButyId(Long.valueOf(12345645));
user.setButyName("yangchuan");
user.setButyPassword("12345");
getUserManage().addUser(user);
//excuteJDBCTranscation();
}
}
本人才疏学浅,涉java不深,如果以上配置有什么错误或不足的地方,请您指出,将 不甚感激!如果还有别的配置,也请共享一份给我,将感激不尽。
说明:如果你的代码报:java.lang.ClassCastException: $Proxy7 cannot be cast to 。。。。。的错误;
解决方式有两种:
一种: 为org.springframework.transaction.interceptor.TransactionProxyFactoryBean的bean配置一个property属性:proxyTargetClass ,并设置其值为true
另一种:就是为你的dao类写一个接口类,就是说让你的dao必须实现一个接口。