2017-06-13 144 views
0

我從this guide開始,將我們的xml配置遷移到註釋配置。帶有多個JPA持久性單元的Spring Annotation配置不會持久

當前問題是,我的測試持久似乎並沒有實際寫入數據(=沒有交易提交)。這導致下一次檢查失敗。該環境目前有五個持久性單元,並根據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; 
} 

但我不知道爲什麼。