我從升級的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>
非常感謝您的幫助!