Java Code Examples for org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo#setTransactionType()

The following examples show how to use org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo#setTransactionType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocalContainerEntityManagerFactoryBeanTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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();
}
 
Example 2
Source File: LocalContainerEntityManagerFactoryBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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();
}
 
Example 3
Source File: LocalContainerEntityManagerFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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();
}