use of jakarta.persistence.EntityManager in project spring-boot by spring-projects.
the class TestEntityManager method getEntityManager.
/**
* Return the underlying {@link EntityManager} that's actually used to perform all
* operations.
* @return the entity manager
*/
public final EntityManager getEntityManager() {
EntityManager manager = EntityManagerFactoryUtils.getTransactionalEntityManager(this.entityManagerFactory);
Assert.state(manager != null, "No transactional EntityManager found, is your test running in a transaction?");
return manager;
}
use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class ApplicationManagedEntityManagerIntegrationTests method testEntityManagerProxyAcceptsProgrammaticTxJoining.
@Test
public void testEntityManagerProxyAcceptsProgrammaticTxJoining() {
EntityManager em = entityManagerFactory.createEntityManager();
em.joinTransaction();
}
use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class ApplicationManagedEntityManagerIntegrationTests method testReuseInNewTransaction.
@Test
public void testReuseInNewTransaction() {
EntityManager em = entityManagerFactory.createEntityManager();
em.joinTransaction();
doInstantiateAndSave(em);
endTransaction();
assertThat(em.getTransaction().isActive()).isFalse();
startNewTransaction();
// Call any method: should cause automatic tx invocation
assertThat(em.contains(new Person())).isFalse();
assertThat(em.getTransaction().isActive()).isFalse();
em.joinTransaction();
assertThat(em.getTransaction().isActive()).isTrue();
doInstantiateAndSave(em);
setComplete();
// Should rollback
endTransaction();
assertThat(countRowsInTable(em, "person")).as("Tx must have committed back").isEqualTo(1);
// Now clean up the database
startNewTransaction();
em.joinTransaction();
deleteAllPeopleUsingEntityManager(em);
assertThat(countRowsInTable(em, "person")).as("People have been killed").isEqualTo(0);
setComplete();
}
use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class ApplicationManagedEntityManagerIntegrationTests method testRollbackOccurs.
@Test
public void testRollbackOccurs() {
EntityManager em = entityManagerFactory.createEntityManager();
em.joinTransaction();
doInstantiateAndSave(em);
// Should rollback
endTransaction();
assertThat(countRowsInTable(em, "person")).as("Tx must have been rolled back").isEqualTo(0);
}
use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.
the class EntityManagerFactoryUtilsTests method testDoGetEntityManagerWithTx.
@Test
public void testDoGetEntityManagerWithTx() throws Exception {
try {
EntityManagerFactory factory = mock(EntityManagerFactory.class);
EntityManager manager = mock(EntityManager.class);
TransactionSynchronizationManager.initSynchronization();
given(factory.createEntityManager()).willReturn(manager);
// no tx active
assertThat(EntityManagerFactoryUtils.doGetTransactionalEntityManager(factory, null)).isSameAs(manager);
assertThat(((EntityManagerHolder) TransactionSynchronizationManager.unbindResource(factory)).getEntityManager()).isSameAs(manager);
} finally {
TransactionSynchronizationManager.clearSynchronization();
}
assertThat(TransactionSynchronizationManager.getResourceMap().isEmpty()).isTrue();
}
Aggregations