spring的事务功能不起作用?

在CustomerServiceImp类的add()方法里先给customers表里插入一条数据,然后在往Customerdaily表里插入一条数据,由于在Customerdaily.hbm.xml里配置的主键生成是根据customers的主键生成,两张表共用相同的主键,由于customerdaily表的Name属性定义为NOU NULL,在add()方法里没有给CustomerDaily对象的Name属性赋值,这样数据库肯定会抛异常,正常的话,customers和Customerdaily里都不会插入记录,但是程序完执行的时候,customers表里有一条记录,CustomerServiceImp类其实是TransactionProxyFactoryBean类,也就是CustomerServiceImp的代理,但是我用System.out.println(customerService.getClass().getName());语句输出的结果却还是com.ajax.service.CustomerServiceImp,为什么spring的事务在这里没有起作用呢?


customerService类的定义如下,在add()方上加入事务处理,:

package com.ajax.service;

import java.util.List;

import com.ajax.dao.CustomerDailyDao;
import com.ajax.dao.CustomerDao;
import com.ajax.pojos.Customer;
import com.ajax.pojos.Customerdaily;

public class CustomerServiceImp implements CustomerService{
 private CustomerDao customerDao;
 private CustomerDailyDao customerDailyDao;
 public void add(Customer customer) throws Exception {
  // TODO Auto-generated method stub
  Customerdaily customerDaily=new Customerdaily();
//  customerDaily.setName(customer.getName());//注释
  customer.setCustomerDaily(customerDaily);
  customerDaily.setCustomer(customer);
  customerDao.addCustomer(customer);
 }

 }

数据库表的DDL是这样定义:


    create table `ajax`.`customers`(
        `id` int not null auto_increment,
       `name` varchar(20) default '' not null unique,
       `dataofbirthday` date default '' not null,
       `sex` varchar(1) default '' not null,
       `address` varchar(40) default '' not null,
       `amount_water` double,
       `amount_elic` double,
       `updatedate` varchar(20) default '' not null,
       `remark` varchar(100),
        primary key (`id`)
    );


    create table `ajax`.`customerdaily`(
        `id` int not null auto_increment,
       `name` varchar(20) default '' not null unique,
       `usedwaterinmonth` double,
       `usedelicinmonth` double,
       `managecharge` double,
        primary key (`id`)
    );

    alter table `ajax`.`customerdaily` 
        add index `customerdaily_ibfk_1`(`id`),
        add constraint `customerdaily_ibfk_1`
        foreign key (`id`)
        references `ajax`.`customers`(`id`);
    create index `PRIMARY` on `ajax`.`customerdaily`(`id`);
    create index `name` on `ajax`.`customerdaily`(`name`);

    customers表的hibernate的配置如下:

 <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="com.ajax.pojos.Customer" table="customers" >
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="native" />
        </id>
        <one-to-one name="customerDaily" class="com.ajax.pojos.Customerdaily" cascade="all" outer-join="true"/>
        <property name="name" type="java.lang.String">
            <column name="name" length="20" not-null="true" />
        </property>
        <property name="dataofbirthday" type="java.sql.Date">
            <column name="dataofbirthday" length="10" not-null="true" />
        </property>
        <property name="sex" type="java.lang.String">
            <column name="sex" length="1" not-null="true" />
        </property>
        <property name="address" type="java.lang.String">
            <column name="address" length="40" not-null="true" />
        </property>
        <property name="amountWater" type="java.lang.Double">
            <column name="amount_water" precision="22" scale="0" />
        </property>
        <property name="amountElic" type="java.lang.Double">
            <column name="amount_elic" precision="22" scale="0" />
        </property>
        <property name="updatedate" type="java.lang.String">
            <column name="updatedate" length="20" not-null="true" />
        </property>
        <property name="remark" type="java.lang.String">
            <column name="remark" length="100" />
        </property>     
    </class>
</hibernate-mapping>

 

Customerdaily表的hibernate的配置如下:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
    <class name="com.ajax.pojos.Customerdaily" table="customerdaily">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="foreign">
             <param name="property">customer</param>
            </generator>
        </id>
        <one-to-one name="customer" class="com.ajax.pojos.Customer" constrained="true"/>
        <property name="name" type="java.lang.String">
            <column name="name" length="20" not-null="true" />
        </property>
        <property name="usedwaterinmonth" type="java.lang.Double">
            <column name="usedwaterinmonth" precision="22" scale="0" />
        </property>
        <property name="usedelicinmonth" type="java.lang.Double">
            <column name="usedelicinmonth" precision="22" scale="0" />
        </property>
        <property name="managecharge" type="java.lang.Double">
            <column name="managecharge" precision="22" scale="0" />
        </property>
    </class>
</hibernate-mapping>

 

 

spring的配置文件是这样的


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
 <import resource="springDao.xml"/>
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref bean="sessionFactory"/>
  </property>
 </bean>
 <bean id="customerService" class="com.ajax.service.CustomerServiceImp">
  <property name="customerDao">
   <ref bean="customerDao"/>
  </property>
  <property name="customerDailyDao">
   <ref bean="customerDailyDao"/>
  </property>
 </bean>
 <bean id="customerServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="transactionManager"/>
  </property>
  <property name="target">
   <ref local="customerService"/>
  </property>
  <property name="transactionAttributes">
   <props>
    <prop key="create*">PROPAGATION_REQUIRED</prop>
    <prop key="add*">PROPAGATION_REQUIRED,ISOLATION_READ_COMMITTED</prop>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
   </props>
  </property>
 </bean>
</beans>


 
 
 
  
 
 
 
 
  
 
 
  
 
 
 
 
  
 
 
  
 
 
  
   
   
       
           
           
       
       
       
           
       
       
           
       
       
           
       
       
           
       
       
           
       
       
           
       
       
           
       
       
           
             
   

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值