Search in sources :

Example 31 with EntityManager

use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.

the class SharedEntityManagerCreatorTests method deferredQueryWithUpdate.

@Test
public void deferredQueryWithUpdate() {
    EntityManagerFactory emf = mock(EntityManagerFactory.class);
    EntityManager targetEm = mock(EntityManager.class);
    Query query = mock(Query.class);
    given(emf.createEntityManager()).willReturn(targetEm);
    given(targetEm.createQuery("x")).willReturn(query);
    given(targetEm.isOpen()).willReturn(true);
    EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
    em.createQuery("x").executeUpdate();
    verify(query).executeUpdate();
    verify(targetEm).close();
}
Also used : EntityManager(jakarta.persistence.EntityManager) StoredProcedureQuery(jakarta.persistence.StoredProcedureQuery) Query(jakarta.persistence.Query) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) Test(org.junit.jupiter.api.Test)

Example 32 with EntityManager

use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.

the class SharedEntityManagerCreatorTests method transactionRequiredExceptionOnJoinTransaction.

@Test
public void transactionRequiredExceptionOnJoinTransaction() {
    EntityManagerFactory emf = mock(EntityManagerFactory.class);
    EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
    assertThatExceptionOfType(TransactionRequiredException.class).isThrownBy(em::joinTransaction);
}
Also used : EntityManager(jakarta.persistence.EntityManager) TransactionRequiredException(jakarta.persistence.TransactionRequiredException) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) Test(org.junit.jupiter.api.Test)

Example 33 with EntityManager

use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.

the class SharedEntityManagerCreatorTests method deferredStoredProcedureQueryWithNamedParameters.

@Test
public void deferredStoredProcedureQueryWithNamedParameters() {
    EntityManagerFactory emf = mock(EntityManagerFactory.class);
    EntityManager targetEm = mock(EntityManager.class);
    StoredProcedureQuery query = mock(StoredProcedureQuery.class);
    given(emf.createEntityManager()).willReturn(targetEm);
    given(targetEm.createStoredProcedureQuery("x")).willReturn(query);
    willReturn("y").given(query).getOutputParameterValue("a");
    willReturn("z").given(query).getOutputParameterValue("c");
    given(targetEm.isOpen()).willReturn(true);
    EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
    StoredProcedureQuery spq = em.createStoredProcedureQuery("x");
    spq.registerStoredProcedureParameter("a", String.class, ParameterMode.OUT);
    spq.registerStoredProcedureParameter("b", Number.class, ParameterMode.IN);
    spq.registerStoredProcedureParameter("c", Object.class, ParameterMode.INOUT);
    spq.execute();
    assertThat(spq.getOutputParameterValue("a")).isEqualTo("y");
    assertThatIllegalArgumentException().isThrownBy(() -> spq.getOutputParameterValue("b"));
    assertThat(spq.getOutputParameterValue("c")).isEqualTo("z");
    verify(query).registerStoredProcedureParameter("a", String.class, ParameterMode.OUT);
    verify(query).registerStoredProcedureParameter("b", Number.class, ParameterMode.IN);
    verify(query).registerStoredProcedureParameter("c", Object.class, ParameterMode.INOUT);
    verify(query).execute();
    verify(targetEm).close();
    verifyNoMoreInteractions(query);
    verifyNoMoreInteractions(targetEm);
}
Also used : EntityManager(jakarta.persistence.EntityManager) StoredProcedureQuery(jakarta.persistence.StoredProcedureQuery) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) Test(org.junit.jupiter.api.Test)

Example 34 with EntityManager

use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.

the class SharedEntityManagerCreatorTests method deferredQueryWithSingleResult.

@Test
public void deferredQueryWithSingleResult() {
    EntityManagerFactory emf = mock(EntityManagerFactory.class);
    EntityManager targetEm = mock(EntityManager.class);
    Query query = mock(Query.class);
    given(emf.createEntityManager()).willReturn(targetEm);
    given(targetEm.createQuery("x")).willReturn(query);
    given(targetEm.isOpen()).willReturn(true);
    EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
    em.createQuery("x").getSingleResult();
    verify(query).getSingleResult();
    verify(targetEm).close();
}
Also used : EntityManager(jakarta.persistence.EntityManager) StoredProcedureQuery(jakarta.persistence.StoredProcedureQuery) Query(jakarta.persistence.Query) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) Test(org.junit.jupiter.api.Test)

Example 35 with EntityManager

use of jakarta.persistence.EntityManager in project spring-framework by spring-projects.

the class SharedEntityManagerFactoryTests method testValidUsage.

@Test
public void testValidUsage() {
    Object o = new Object();
    EntityManager mockEm = mock(EntityManager.class);
    given(mockEm.isOpen()).willReturn(true);
    EntityManagerFactory mockEmf = mock(EntityManagerFactory.class);
    given(mockEmf.createEntityManager()).willReturn(mockEm);
    SharedEntityManagerBean proxyFactoryBean = new SharedEntityManagerBean();
    proxyFactoryBean.setEntityManagerFactory(mockEmf);
    proxyFactoryBean.afterPropertiesSet();
    assertThat(EntityManager.class.isAssignableFrom(proxyFactoryBean.getObjectType())).isTrue();
    assertThat(proxyFactoryBean.isSingleton()).isTrue();
    EntityManager proxy = proxyFactoryBean.getObject();
    assertThat(proxyFactoryBean.getObject()).isSameAs(proxy);
    assertThat(proxy.contains(o)).isFalse();
    boolean condition = proxy instanceof EntityManagerProxy;
    assertThat(condition).isTrue();
    EntityManagerProxy emProxy = (EntityManagerProxy) proxy;
    assertThatIllegalStateException().as("outside of transaction").isThrownBy(emProxy::getTargetEntityManager);
    TransactionSynchronizationManager.bindResource(mockEmf, new EntityManagerHolder(mockEm));
    try {
        assertThat(emProxy.getTargetEntityManager()).isSameAs(mockEm);
    } finally {
        TransactionSynchronizationManager.unbindResource(mockEmf);
    }
    assertThat(TransactionSynchronizationManager.getResourceMap().isEmpty()).isTrue();
    verify(mockEm).contains(o);
    verify(mockEm).close();
}
Also used : EntityManager(jakarta.persistence.EntityManager) EntityManagerFactory(jakarta.persistence.EntityManagerFactory) EntityManagerHolder(org.springframework.orm.jpa.EntityManagerHolder) EntityManagerProxy(org.springframework.orm.jpa.EntityManagerProxy) 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