我看到很多問題,但他們的所有解決方案仍然不清楚。 這裏是我的設置 我的Spring上下文文件Spring - JPA -m @ Transactional沒有提交數據DB
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
..
...
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
...
...
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="entityManagerFactory1"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
autowire="byName">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="packagesToScan" value="com.my.dom.domain" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle10gDialect
</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="persistenceUnitName" value="PersistenceUnit1"></property>
</bean>
<bean id="EntityManagerFactory2"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
autowire="byName">
<property name="dataSource" ref="myDataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="packagesToScan" value="com.my.dom.domain" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.H2Dialect
</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.jdbc.fetch_size">50</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
..
...
<bean id="entityManager1" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory1" />
</bean>
<tx:annotation-driven transaction-manager="entityManager1" />
<bean id="EntityManager2" factory-bean="EntityManagerFactory2" factory-method="createEntityManager" autowire="byName" scope="prototype"/>
<tx:annotation-driven transaction-manager="EntityManager2" />
<context:component-scan base-package="com.my.dom" />
<jpa:repositories base-package="com.my.dom.repository"
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager" />
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
</beans>
所有的數據源和其他連接豆類到位並正常工作。
我的DAO代碼如下 package com.my.dom.dao;
@Component
public class MyDAOImpl implements MyDAO {
@PersistenceContext(unitName="PersistenceUnit1")
EntityManager entityManager; // this is IAR EntityManager.
@Override
@Transactional
public boolean saveMyGroups(
List<MyGroups> theGroups) {
logger.info("Entering method - saveFuturetheGroups ");
//entityManager.getTransaction().begin();
for(MyGroups pg : theGroups){
MyGroups attachedENtity = entityManager.merge(pg);
entityManager.persist(pg);
}
//entityManager.getTransaction().commit();
entityManager.flush();
logger.info("Exiting method - saveFuturetheGroups ");
//List<MyGroups> savedEntities = futureProcGrpRepository.save(theGroups);
return true;
}
}
我所有的CGLIB並在類路徑aopalliance1.0.jars在Spring+JPA @Transactional not committing
建議還是我的代碼沒有提交修改爲DB。任何幫助指針都會非常有幫助。
該代碼顯示了select和insert sql語句,並沒有拋出異常但代碼沒有提交。 @Transactional註釋中是否存在任何問題?
可以定義多個entitymanagers在這裏的任何問題?
確定什麼我發現是,如果我使用 @Transactional(傳播= Propagation.REQUIRES_NEW)
對我的方法的前頭提交更改!但根據我的理解,傳播的默認值是'REQUIRED',任何方式都將啓動新的轉換,並應在最後提交?任何指針,我現在真的很困惑!
感謝 HK
剛一說明,我從JUnit測試情況下運行這一點,是有可能,它明確地回滾的變化?我看到類似日誌
INFO : org.springframework.test.context.transaction.TransactionalTestExecutionListener - Rolled back transaction after test execution for test context [[[email protected] testClass = ProcessingGroupsTestCase, testInstance = null(com.wellmanage.dos.processingGroups.ProcessingGroupsTestCase), testMethod = [email protected], testException = [null], mergedContextConfiguration = [[email protected] testClass = ProcessingGroupsTestCase, locations = '{classpath:applicationContext.xml}', classes = '{}', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader']]]
感謝 以下HK
假設你的'Entity'類沒有被定義爲'insert = false,update = false'等 – user75ponic
嗨,在我的實體中沒有這樣的屬性。 – user2482019
是否存在從事務塊 –