P.S 这里很多Hibernate和Spring配置文件是隐藏的,只有一些重要的文件显示,如果你想看全部文件,请在文章的结尾下载完整的项目代码。
CREATE TABLE `zyiz`.`product` ( `PRODUCT_ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `PRODUCT_CODE` varchar(20) NOT NULL, `PRODUCT_DESC` varchar(255) NOT NULL, PRIMARY KEY (`PRODUCT_ID`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; CREATE TABLE `zyiz`.`product_qoh` ( `QOH_ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `PRODUCT_ID` bigint(20) unsigned NOT NULL, `QTY` int(10) unsigned NOT NULL, PRIMARY KEY (`QOH_ID`), KEY `FK_product_qoh_product_id` (`PRODUCT_ID`), CONSTRAINT `FK_product_qoh_product_id` FOREIGN KEY (`PRODUCT_ID`) REFERENCES `product` (`PRODUCT_ID`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
package com.zyiz.product.bo.impl; import com.zyiz.product.bo.ProductBo; import com.zyiz.product.bo.ProductQohBo; import com.zyiz.product.dao.ProductDao; import com.zyiz.product.model.Product; import com.zyiz.product.model.ProductQoh; public class ProductBoImpl implements ProductBo{ ProductDao productDao; ProductQohBo productQohBo; public void setProductDao(ProductDao productDao) { this.productDao = productDao; } public void setProductQohBo(ProductQohBo productQohBo) { this.productQohBo = productQohBo; } //this method need to be transactional public void save(Product product, int qoh){ productDao.save(product); System.out.println("Product Inserted"); ProductQoh productQoh = new ProductQoh(); productQoh.setProductId(product.getProductId()); productQoh.setQty(qoh); productQohBo.save(productQoh); System.out.println("ProductQoh Inserted"); } }
<!-- Product business object --> <bean id="productBo" class="com.zyiz.product.bo.impl.ProductBoImpl" > <property name="productDao" ref="productDao" /> <property name="productQohBo" ref="productQohBo" /> </bean> <!-- Product Data Access Object --> <bean id="productDao" class="com.zyiz.product.dao.impl.ProductDaoImpl" > <property name="sessionFactory" ref="sessionFactory"></property> </bean>
运行它
Product product = new Product(); product.setProductCode("ABC"); product.setProductDesc("This is product ABC"); ProductBo productBo = (ProductBo)appContext.getBean("productBo"); productBo.save(product, 100);
假设save() 不具有事务功能,如果异常抛出由productQohBo.save(),钭只插入一条记录到“product”表,没有记录将被插入到“productQoh'表。这是一个严重的问题,在数据库中打破数据一致性。
<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.5.xsd"> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="transactionManager" /> <property name="transactionAttributes"> <props> <prop key="save">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="dataSource" ref="dataSource" /> <property name="sessionFactory" ref="sessionFactory" /> </bean> </beans>
在事务拦截器,必须定义的事务的属性“传播行为”应使用。这意味着,如果一个事务“ProductBoImpl.save()方法被调用另外的”productQohBo.save()'方法,事务应该怎么传播?它能继续在现有的事务中运行?或者为自己开始一个新的事务。
在Hibernate事务,需要使用 HibernateTransactionManager 。 如果只对付纯JDBC,useDataSourceTransactionManager; 而如果是 JTA,需要使用 JtaTransactionManager 。
<!-- Product business object --> <bean id="productBo" class="com.zyiz.product.bo.impl.ProductBoImpl" > <property name="productDao" ref="productDao" /> <property name="productQohBo" ref="productQohBo" /> </bean> <!-- Product Data Access Object --> <bean id="productDao" class="com.zyiz.product.dao.impl.ProductDaoImpl" > <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="productBoProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="productBo" /> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> </bean>
运行它
Product product = new Product(); product.setProductCode("ABC"); product.setProductDesc("This is product ABC"); ProductBo productBo = (ProductBo)appContext.getBean("productBoProxy"); productBo.save(product, 100);