Search in sources :

Example 16 with EntityManager

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

the class SharedEntityManagerCreatorTests method deferredStoredProcedureQueryWithIndexedParameters.

@Test
public void deferredStoredProcedureQueryWithIndexedParameters() {
    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(0);
    willReturn("z").given(query).getOutputParameterValue(2);
    given(targetEm.isOpen()).willReturn(true);
    EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
    StoredProcedureQuery spq = em.createStoredProcedureQuery("x");
    spq.registerStoredProcedureParameter(0, String.class, ParameterMode.OUT);
    spq.registerStoredProcedureParameter(1, Number.class, ParameterMode.IN);
    spq.registerStoredProcedureParameter(2, Object.class, ParameterMode.INOUT);
    spq.execute();
    assertThat(spq.getOutputParameterValue(0)).isEqualTo("y");
    assertThatIllegalArgumentException().isThrownBy(() -> spq.getOutputParameterValue(1));
    assertThat(spq.getOutputParameterValue(2)).isEqualTo("z");
    verify(query).registerStoredProcedureParameter(0, String.class, ParameterMode.OUT);
    verify(query).registerStoredProcedureParameter(1, Number.class, ParameterMode.IN);
    verify(query).registerStoredProcedureParameter(2, 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 17 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 18 with EntityManager

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

the class ApplicationManagedEntityManagerIntegrationTests method testEntityManagerProxyIsProxy.

@Test
@SuppressWarnings("unchecked")
public void testEntityManagerProxyIsProxy() {
    EntityManager em = entityManagerFactory.createEntityManager();
    assertThat(Proxy.isProxyClass(em.getClass())).isTrue();
    Query q = em.createQuery("select p from Person as p");
    List<Person> people = q.getResultList();
    assertThat(people).isNotNull();
    assertThat(em.isOpen()).as("Should be open to start with").isTrue();
    em.close();
    assertThat(em.isOpen()).as("Close should work on application managed EM").isFalse();
}
Also used : EntityManager(jakarta.persistence.EntityManager) Query(jakarta.persistence.Query) Person(org.springframework.orm.jpa.domain.Person) Test(org.junit.jupiter.api.Test)

Example 19 with EntityManager

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

the class ApplicationManagedEntityManagerIntegrationTests method testCannotFlushWithoutGettingTransaction.

@Test
public void testCannotFlushWithoutGettingTransaction() {
    EntityManager em = entityManagerFactory.createEntityManager();
    assertThatExceptionOfType(TransactionRequiredException.class).isThrownBy(() -> doInstantiateAndSave(em));
    // TODO following lines are a workaround for Hibernate bug
    // If Hibernate throws an exception due to flush(),
    // it actually HAS flushed, meaning that the database
    // was updated outside the transaction
    deleteAllPeopleUsingEntityManager(sharedEntityManager);
    setComplete();
}
Also used : EntityManager(jakarta.persistence.EntityManager) TransactionRequiredException(jakarta.persistence.TransactionRequiredException) Test(org.junit.jupiter.api.Test)

Example 20 with EntityManager

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

the class ApplicationManagedEntityManagerIntegrationTests method testInstantiateAndSave.

@Test
public void testInstantiateAndSave() {
    EntityManager em = entityManagerFactory.createEntityManager();
    em.joinTransaction();
    doInstantiateAndSave(em);
}
Also used : EntityManager(jakarta.persistence.EntityManager) Test(org.junit.jupiter.api.Test)

Aggregations

EntityManager (jakarta.persistence.EntityManager)55 Test (org.junit.jupiter.api.Test)52 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)25 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