Search in sources :

Example 1 with StoredProcedureQuery

use of jakarta.persistence.StoredProcedureQuery 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 2 with StoredProcedureQuery

use of jakarta.persistence.StoredProcedureQuery 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)

Aggregations

EntityManager (jakarta.persistence.EntityManager)2 EntityManagerFactory (jakarta.persistence.EntityManagerFactory)2 StoredProcedureQuery (jakarta.persistence.StoredProcedureQuery)2 Test (org.junit.jupiter.api.Test)2