springmvc+Hibernate+JPA (hybrid transactions)
Recently I found that spring-data-jpa is easier to use.
I also tried adding jpa in springmvc after using it in springcloud's project.
However, the old project uses hibernate. After using jpa to add, some problems occurred in the transaction.
Solution
Configuration File
1 Configure Hibernate transactions (transactionManager)
<!-- Things Manager Configuration --> <bean class="."> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <!-- hibernate -->
2 Configure Jpa transactions (transactionManager_jpa)
<!-- Jpa Transaction configuration --> <bean class=""> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <!-- Spring Data JpaConfiguration --> <jpa:repositories base-package=".*" transaction-manager-ref="transactionManager_jpa" entity-manager-factory-ref="entityManagerFactory"/> <!-- useannotationDefine transactions --> <tx:annotation-driven transaction-manager="transactionManager_jpa" proxy-target-class="true" />
Configuration full version:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="/schema/beans" xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:context="/schema/context" xmlns:mvc="/schema/mvc" xmlns:aop="/schema/aop" xmlns:tx="/schema/tx" xmlns:p="/schema/p" xmlns:jpa="/schema/data/jpa" xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-4. /schema/context /schema/context/spring-context-4. /schema/mvc /schema/mvc/spring-mvc-4. /schema/aop /schema/aop/spring-aop-4. /schema/tx /schema/tx/spring-tx-4. /schema/data/jpa /schema/data/jpa/" default-lazy-init="true"> <!--Configure data source--> <bean class=".v2." destroy-method="close"> <property name="driverClass" value="${}" /> <!--Database connection driver--> <property name="jdbcUrl" value="${}" /> <!--Database address--> <property name="user" value="${}" /> <!--username--> <property name="password" value="${}" /> <!--password--> <property name="maxPoolSize" value="${}" /> <!--Maximum number of connections--> <property name="minPoolSize" value="${}" /> <!--Minimum number of connections--> <property name="initialPoolSize" value="${}" /> <!--Initialize the database connection in the connection pool--> <property name="maxIdleTime" value="${}" /> <!--Maximum free time--> </bean> <!-- hibernate --> <!--Configurationsessionfactory--> <bean class="."> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="" /> <property name="hibernateProperties"> <props> <prop key="hibernate.">${hibernate.}</prop> <!--hibernateAutomatically generate database tables based on entities--> <prop key="">${}</prop> <!--Specify database dialect--> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <!--Display the executed database operation statement on the console--> <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> <!--Display the executed data cry operation statement on the console(Format)--> <prop key=".use_second_level_cache">${.use_second_level_cache}</prop> <prop key=".use_query_cache">${.use_query_cache}</prop> <!-- Query cache --> <prop key=".provider_class">${.provider_class}</prop> <prop key=".factory_class">${.factory_class}</prop> </props> </property> </bean> <!-- 事物管理器Configuration --> <bean class="."> <property name="sessionFactory" ref="sessionFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> <!-- hibernate --> <!-- JPA --> <!-- JPA实体管理器factory --> <bean name="jpaEntityManagerFactory" class=""> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" /> <!-- Add custom package path --> <property name="packagesToScan" value=".*" /> <property name="jpaProperties"> <props> <prop key="hibernate.current_session_context_class">thread</prop> <prop key="hibernate.">none</prop><!-- validate/update/create --> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <!-- Naming rules for building tables --> <prop key=".naming_strategy"></prop> </props> </property> </bean> <!-- set upJPAImplement the specific properties of the manufacturer --> <bean class=""> <property name="databasePlatform" value="${}"/> </bean> <!-- Jpa 事务Configuration --> <bean class=""> <property name="entityManagerFactory" ref="entityManagerFactory"/> </bean> <!-- Spring Data JpaConfiguration --> <jpa:repositories base-package=".*" transaction-manager-ref="transactionManager_jpa" entity-manager-factory-ref="entityManagerFactory"/> <!-- useannotationDefine transactions --> <tx:annotation-driven transaction-manager="transactionManager_jpa" proxy-target-class="true" /> <!-- JPA --> </beans>
Modify @Transactional
When using JPA, indicate
@Transactional("transactionManager_jpa")
@Transactional("transactionManager_jpa") @Override public Model_Res add(Model_Req req) { //jpa dao Model entity = (reqData); return res; }
The original Hibernate doesn't need to be changed (because)
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.