2014-02-17 68 views
0

我正在处理一个导入模块,在这里我从一个excel文件中读取数据,填充实体并保存它们。春季和春季的Tansactional上下文data​​-jpa

现在我的麻烦是,我正在与许多实体工作,然后我用三种不同的方法分解保存在数据库。

这是我的导入方法:

@Transactional(propagation = Propagation.REQUIRES_NEW) 
private void readRowFromExcel(Row row){ 

    for(Row row : sheet){ 

     Customer customer = readCustomerFromExcel(row) 
     CustomerDeatails customerDetails = readCustomerDetailsFromExcel(row)  

     customerService.save(customer,customerDetails); 

     MyEntity myEntity = readMyEntityFromExcel(row); 

     myEntityService.save(myEntity) 

    } 


} 

这是在客户服务

@Transactional(propagation = Propagation.REQUIRED) 
public void save(Customer customer, CustomerDetails customerDetails){ 

      customerRepository.save(customer); 
      customerDatailsRepository.save(customer); 

} 

保存方法这是保存方法在myEntity所服务:

@Transactional(propagation = Propagation.REQUIRED) 
public void save(MyEntity myEntity){ 

    myEntityRepository.save(myEntity) 

} 

现在我的麻烦是,如果我在下面的代码行

MyEntity myEntity = readMyEntityFromExcel(row);

实体客户和为CustomerDetails已经是持久的,而我应该只保存如果一切正常

我春天的context.xml

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="close"> 
     <property name="driverClassName" value="${db.driverClass}" /> 
     <property name="url" value="${db.connectionURL}" /> 
     <property name="username" value="${db.username}" /> 
     <property name="password" value="${db.password}" /> 
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" 
     p:entityManagerFactory-ref="entityManagerFactory"> 
     <property name="persistenceUnitName" value="myUnit" /> 
    </bean> 

    <!-- 
    <bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
     <description>Allow spring to configure hibernate specific settings</description> 
    </bean> 
    --> 
    <bean id="eclipseLinkJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"> 
    </bean> 

    <bean id="entityManagerFactory" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" 
     p:dataSource-ref="dataSource" 
     p:persistenceUnitName="myUnit" 
     p:jpaVendorAdapter-ref="eclipseLinkJpaVendorAdapter"> 
     <property name="jpaPropertyMap"> 
      <map> 
       <entry key="eclipselink.cache.shared.default" value="false" /> 
       <entry key="eclipselink.weaving" value="false" /> 
      </map> 
     </property> 
     <property name="loadTimeWeaver"> 
      <bean 
       class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /> 
     </property> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager" /> 

我该怎么做才能解决呢?

回答

0

解决方法很简单:你的任何DB持续存在的移动之前从读取Excel行由:

@Transactional(propagation = Propagation.REQUIRES_NEW) 
private void readRowFromExcel(Row row){ 

    for(Row row : sheet){ 
     //Reading from Excel 
     Customer customer = readCustomerFromExcel(row) 
     CustomerDeatails customerDetails = readCustomerDetailsFromExcel(row); 
     MyEntity myEntity = readMyEntityFromExcel(row); 

     //DB persisting 
     customerService.save(customer,customerDetails); 
     myEntityService.save(myEntity) 
    } 

} 
+0

如果一个异常发生时,我尽量节省(myEntity所)? – Skizzo

+0

因为你在交易中,所有的东西都应该回滚。如果情况并非如此,那么您的数据源可能不是JTA,或者您的数据库表不支持事务处理。 –