2017-12-18 294 views
0

我从升级的Web应用程序的原始对象:休眠5.x和春季数据2.X:如何在服务更新的保存方法

春季(MVC)4.2.9.RELEASE,休眠4.3 .8.Final和1.7.1.RELEASE

春季(MVC)5.0.2.RELEASE,休眠5.2.12.Final和Spring数据2.0.2.RELEASE。

Web应用程序在Windows和MS SQL Server 2014 Enterprise上运行。

升级并没有强迫我改变Hibernate和JPA的设置。但是,现在该程序的行为非常不同,这在下面进行了解释。

以下是应用程序中的一种典型服务方法,该方法保存一个新的Account对象,然后更新其值,如果它是新对象,则返回它。帐户对象ID字段定义如下:

@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
private Long id; 

这是役方法:

@Override 
@Transactional 
public Account saveAccount(Account acc) { 

    //Step 1 
    //save method from Spring Data 
    accountRepository.save(acc); 

    //stringId is set manually only once and it is set only when a new accout object is created 
    if (acc.getStringId() == null) { 

     //Step 2 
     acc.setStringId("some_string_id"); 
    } 

    return acc; 
} 

这里是在升级前的行为:

步骤1:ACC是相同的保存后的对象。保存后,其id字段从null更新为Long值。

第2步:新值自动序列化到数据库。请注意,没有明确的数据库调用save方法。

这里是升级后的行为:

步骤1:使用accountRepository.save(acc)只是不更新​​ACC对象。要获得新的ID的对象,我一定要做到以下方式:

acc = accountRepository.save(acc) 

步骤2:将字符串ID不保存为一个新的对象。

我正在寻找方法使系统在升级之前按照这种方式工作。原因是应用程序不是微不足道的,而且我在应用程序中遵循了编程模式(好或坏)。我想避免很多更改和重新测试。

这里是相关配置

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
     xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
     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-3.2.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.2.xsd 
      http://www.springframework.org/schema/data/jpa  
      http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd        
      http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
      http://www.springframework.org/schema/jdbc 
      http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd"> 

    <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close"> 
     <constructor-arg ref="hikariConfig" /> 
    </bean> 

    <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig"> 
     <property name="poolName" value="derek6HikariCP" /> 
     <property name="connectionTestQuery" value="${jdbc.connectionTestQuery}" /> 
     <property name="dataSourceClassName" value="${jdbc.dataSourceClassName}" /> 
     <property name="maximumPoolSize" value="${jdbc.maximumPoolSize}" /> 
     <property name="minimumIdle" value="${jdbc.minimumIdle}" /> 
     <property name="idleTimeout" value="${jdbc.idleTimeout}" /> 
     <property name="connectionTimeout" value="${jdbc.connectionTimeout}" /> 
     <property name="dataSourceProperties"> 
      <props> 
       <prop key="url">${jdbc.url}</prop> 
       <prop key="user">${jdbc.username}</prop> 
       <prop key="password">${jdbc.password}</prop> 
      </props> 
     </property>  
    </bean> 

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
     <constructor-arg ref="dataSource"/> 
    </bean> 

    <bean id="entityManagerFactory" name="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> 
     </property> 
     <property name="packagesToScan" value="myproject.entity" /> 
     <property name="jpaProperties"> 
      <props> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
       <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
       <prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>         
       <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop> 
       <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop> 
      </props> 
     </property> 
    </bean>  

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="emf" /> 
    </bean> 

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

    <context:annotation-config /> 

    <jpa:repositories base-package="myproject.entity" 
     entity-manager-factory-ref="emf" transaction-manager-ref="transactionManager" /> 

</beans> 

非常感谢您的帮助!

回答