2017-06-13 13 views
0

xmlコンフィグレーションをアノテーションコンフィグレーションに移行するために、this guideから始めました。複数のJPAパーシスタンスユニットを持つSpringアノテーションコンフィグレーションが維持されない

現在の問題は、私のテストでは、データが実際に書き込まれていないようです(=トランザクションコミットなし)。この結果、次のチェックが失敗します。現在、環境には5つの永続ユニットがあり、entityManagerFactoriesおよびtransactionManagersに従っています。

MyTest.java

@RunWith(SpringRunner.class) 
@ContextConfiguration(classes = {PersistenceJPAConfig.class}, loader = AnnotationConfigContextLoader.class) 
public class MyTest { 

    @Autowired 
    private MyDao testable; 

    @Transactional(transactionManager = "tm1") // tried with name= and without 
    @Test 
    public void crudTest() throws Exception { 
     // assert that the table is empty 
     List<MyDO> all = testable.getAll(); 
     assertTrue(all.isEmpty()); 

     // write one entity 
     MyDO anEntity = new MyDO(); 
     testable.persist(anEntity); 

     // load all entities and assert that the details match 
     List<MyDO> allAfterInsert = testable.getAll(); 
     // THIS FAILS 
     assertFalse("The database result should not be empty.", allAfterInsert.isEmpty()); 
    } 
} 

PersistenceJPAConfig.java

@Configuration 
@EnableTransactionManagement 
@ComponentScan("my.package") 
public class PersistenceJPAConfig { 

    @PersistenceContext(unitName = "myPersistenceUnit") // also tried 
    @Bean(name = "myEntityManager") 
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 
     LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); 
     em.setDataSource(dataSource()); 
     em.setPackagesToScan(new String[] {"my.package"}); 
     em.setPersistenceUnitName("myPersistenceUnit"); 

     JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); 
     em.setJpaVendorAdapter(vendorAdapter); 
     em.setJpaProperties(additionalProperties()); 

     return em; 
    } 

    // ... the same for the other persistenceUnits with increasing names 

    @Bean 
    public DataSource dataSource() { 
     DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
     dataSource.setDriverClassName("org.h2.Driver"); 
     dataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"); 
     dataSource.setUsername("sa"); 
     dataSource.setPassword(""); 
     return dataSource; 
    } 


    @Bean(name = "tm1") 
    public PlatformTransactionManager transactionManager() { 
     JpaTransactionManager transactionManager = new JpaTransactionManager(); 
     transactionManager.setEntityManagerFactory(entityManagerFactory().getNativeEntityManagerFactory()); 
     transactionManager.setPersistenceUnitName("myPersistenceUnit"); 
     transactionManager.afterPropertiesSet(); 
     return transactionManager; 
    } 

    // ... the same for the other persistenceUnits with increasing names 

    @Bean 
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { 
     return new PersistenceExceptionTranslationPostProcessor(); 
    } 

    @Bean 
    public PersistenceAnnotationBeanPostProcessor persistenceAnnotationBeanPostProcessor() { 
     return new PersistenceAnnotationBeanPostProcessor(); 
    } 

    Properties additionalProperties() { 
     Properties properties = new Properties(); 
     properties.setProperty("hibernate.hbm2ddl.auto", "update"); 
     properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); 
     return properties; 
    } 
} 

MyDao.java

public class MyDao { 

    @PersistenceContext(unitName = "myPersistenceUnit") 
    EntityManager em; 

    @Override 
    @Transactional(transactionManager = "tm1") // also tried with different variations like above 
    public Long persist(final MyDO entity) { 
     em.persist(entity); // tried to add an em.flush(), but that throws a TransactionRequiredException: no transaction is in progress 
     // handling transactions would throw IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead 
     return entity.getId(); 
    } 
} 

ユニットがファイルに配置されている永続META-INF/persistence.xml

テストはxml構成では機能しましたが、現在の注釈設定では機能しません。私が忘れたものはありますか?私はどんな情報を提供することができますか?

答えて

0

解決策が見つかりました:entityManagerFactory().getNativeEntityManagerFactory()の代わりにentityManagerFactory().getObject()を使用しましたが、すべて正常に動作します。

@Bean(name = "tm1") 
public PlatformTransactionManager transactionManager() { 
    JpaTransactionManager transactionManager = new JpaTransactionManager(); 
    transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); 
    transactionManager.setPersistenceUnitName("myPersistenceUnit"); 
    transactionManager.afterPropertiesSet(); 
    return transactionManager; 
} 

私は確信しています。

関連する問題