Search in sources :

Example 1 with MutablePersistenceUnitInfo

use of org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo in project spring-framework by spring-projects.

the class LocalContainerEntityManagerFactoryBeanTests method testApplicationManagedEntityManagerWithJtaTransaction.

@Test
public void testApplicationManagedEntityManagerWithJtaTransaction() throws Exception {
    Object testEntity = new Object();
    // This one's for the tx (shared)
    EntityManager sharedEm = mock(EntityManager.class);
    given(sharedEm.getTransaction()).willReturn(new NoOpEntityTransaction());
    // This is the application-specific one
    EntityManager mockEm = mock(EntityManager.class);
    given(mockEmf.createEntityManager()).willReturn(sharedEm, mockEm);
    LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
    MutablePersistenceUnitInfo pui = ((MutablePersistenceUnitInfo) cefb.getPersistenceUnitInfo());
    pui.setTransactionType(PersistenceUnitTransactionType.JTA);
    JpaTransactionManager jpatm = new JpaTransactionManager();
    jpatm.setEntityManagerFactory(cefb.getObject());
    TransactionStatus txStatus = jpatm.getTransaction(new DefaultTransactionAttribute());
    EntityManagerFactory emf = cefb.getObject();
    assertSame("EntityManagerFactory reference must be cached after init", emf, cefb.getObject());
    assertNotSame("EMF must be proxied", mockEmf, emf);
    EntityManager em = emf.createEntityManager();
    em.joinTransaction();
    assertFalse(em.contains(testEntity));
    jpatm.commit(txStatus);
    cefb.destroy();
    verify(mockEm).joinTransaction();
    verify(mockEm).contains(testEntity);
    verify(mockEmf).close();
}
Also used : EntityManager(javax.persistence.EntityManager) DefaultTransactionAttribute(org.springframework.transaction.interceptor.DefaultTransactionAttribute) EntityManagerFactory(javax.persistence.EntityManagerFactory) TransactionStatus(org.springframework.transaction.TransactionStatus) MutablePersistenceUnitInfo(org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo) Test(org.junit.Test)

Aggregations

EntityManager (javax.persistence.EntityManager)1 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 Test (org.junit.Test)1 MutablePersistenceUnitInfo (org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1 DefaultTransactionAttribute (org.springframework.transaction.interceptor.DefaultTransactionAttribute)1