簡體   English   中英

具有多個JPA持久性單元的Spring注釋配置不持久

[英]Spring Annotation Config with Multiple JPA Persistence Units Doesn't Persist

我從本指南開始,將我們的xml配置遷移到注釋配置。

當前的問題是,我的持久化測試似乎並未實際寫入數據(=沒有事務提交)。 這導致下一次檢查失敗。 該環境當前具有五個持久性單元,並具有相應的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配置,但不適用於我當前的注釋配置。 有什么我忘了嗎? 我還能提供什么信息?

我找到了一個解決方案:我不是使用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;
}

我不確定為什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM