Search in sources :

Example 36 with EntityManager

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;
}
Also used : EntityManager(jakarta.persistence.EntityManager)

Example 37 with EntityManager

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();
}
Also used : EntityManager(jakarta.persistence.EntityManager) Test(org.junit.jupiter.api.Test)

Example 38 with EntityManager

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();
}
Also used : EntityManager(jakarta.persistence.EntityManager) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.jupiter.api.Test)

Example 39 with EntityManager

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);
}
Also used : EntityManager(jakarta.persistence.EntityManager) Test(org.junit.jupiter.api.Test)

Example 40 with EntityManager

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();
}
Also used : EntityManager(jakarta.persistence.EntityManager) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) Test(org.junit.jupiter.api.Test)

Aggregations

EntityManager (jakarta.persistence.EntityManager)58 Test (org.junit.jupiter.api.Test)53 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)26 Query (jakarta.persistence.Query)9 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)9 TransactionRequiredException (jakarta.persistence.TransactionRequiredException)7 Person (org.springframework.orm.jpa.domain.Person)7 StoredProcedureQuery (jakarta.persistence.StoredProcedureQuery)6 Properties (java.util.Properties)5 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)5 EntityManagerHolder (org.springframework.orm.jpa.EntityManagerHolder)5 EntityTransaction (jakarta.persistence.EntityTransaction)4 HashMap (java.util.HashMap)4 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)4 ExpectedLookupTemplate (org.springframework.context.testfixture.jndi.ExpectedLookupTemplate)4 TransactionStatus (org.springframework.transaction.TransactionStatus)4 NoResultException (jakarta.persistence.NoResultException)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 AfterEach (org.junit.jupiter.api.AfterEach)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3