Search in sources :

Example 26 with StoredProcedureQuery

use of javax.persistence.StoredProcedureQuery in project hibernate-orm by hibernate.

the class OracleStoredProcedureTest method testSysRefCursorAsOutParameter.

@Test
@TestForIssue(jiraKey = "HHH-11863")
public void testSysRefCursorAsOutParameter() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        StoredProcedureQuery function = entityManager.createNamedStoredProcedureQuery("singleRefCursor");
        function.execute();
        assertFalse(function.hasMoreResults());
        Long value = null;
        try (ResultSet resultSet = (ResultSet) function.getOutputParameterValue(1)) {
            while (resultSet.next()) {
                value = resultSet.getLong(1);
            }
        } catch (SQLException e) {
            fail(e.getMessage());
        }
        assertEquals(Long.valueOf(1), value);
    });
}
Also used : SQLException(java.sql.SQLException) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery) ResultSet(java.sql.ResultSet) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 27 with StoredProcedureQuery

use of javax.persistence.StoredProcedureQuery in project hibernate-orm by hibernate.

the class OracleStoredProcedureTest method testOutAndSysRefCursorAsOutParameter.

@Test
@TestForIssue(jiraKey = "HHH-11863")
public void testOutAndSysRefCursorAsOutParameter() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        StoredProcedureQuery function = entityManager.createNamedStoredProcedureQuery("outAndRefCursor");
        function.execute();
        assertFalse(function.hasMoreResults());
        Long value = null;
        try (ResultSet resultSet = (ResultSet) function.getOutputParameterValue(1)) {
            while (resultSet.next()) {
                value = resultSet.getLong(1);
            }
        } catch (SQLException e) {
            fail(e.getMessage());
        }
        assertEquals(value, function.getOutputParameterValue(2));
    });
}
Also used : SQLException(java.sql.SQLException) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery) ResultSet(java.sql.ResultSet) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 28 with StoredProcedureQuery

use of javax.persistence.StoredProcedureQuery in project hibernate-orm by hibernate.

the class OracleStoredProcedureTest method testStoredProcedureRefCursor.

@Test
public void testStoredProcedureRefCursor() {
    EntityManager entityManager = createEntityManager();
    entityManager.getTransaction().begin();
    try {
        StoredProcedureQuery query = entityManager.createStoredProcedureQuery("sp_person_phones");
        query.registerStoredProcedureParameter(1, Long.class, ParameterMode.IN);
        query.registerStoredProcedureParameter(2, Class.class, ParameterMode.REF_CURSOR);
        query.setParameter(1, 1L);
        query.execute();
        List<Object[]> postComments = query.getResultList();
        assertNotNull(postComments);
    } finally {
        entityManager.getTransaction().rollback();
        entityManager.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery) Test(org.junit.Test)

Example 29 with StoredProcedureQuery

use of javax.persistence.StoredProcedureQuery in project tutorials by eugenp.

the class StoredProcedureIntegrationTest method findCarsByYearNamedProcedure.

@Test
public void findCarsByYearNamedProcedure() {
    final StoredProcedureQuery findByYearProcedure = entityManager.createNamedStoredProcedureQuery("findByYearProcedure");
    final StoredProcedureQuery storedProcedure = findByYearProcedure.setParameter("p_year", 2015);
    storedProcedure.getResultList().forEach(c -> Assert.assertEquals(new Integer(2015), ((Car) c).getYear()));
}
Also used : Car(com.baeldung.jpa.model.Car) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Test(org.junit.Test)

Example 30 with StoredProcedureQuery

use of javax.persistence.StoredProcedureQuery in project tutorials by eugenp.

the class StoredProcedureIntegrationTest method findCarsByYearNoNamed.

@Test
public void findCarsByYearNoNamed() {
    final StoredProcedureQuery storedProcedure = entityManager.createStoredProcedureQuery("FIND_CAR_BY_YEAR", Car.class).registerStoredProcedureParameter(1, Integer.class, ParameterMode.IN).setParameter(1, 2015);
    storedProcedure.getResultList().forEach(c -> Assert.assertEquals(new Integer(2015), ((Car) c).getYear()));
}
Also used : Car(com.baeldung.jpa.model.Car) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Test(org.junit.Test)

Aggregations

StoredProcedureQuery (javax.persistence.StoredProcedureQuery)60 Test (org.junit.Test)47 EntityManager (javax.persistence.EntityManager)38 NamedStoredProcedureQuery (javax.persistence.NamedStoredProcedureQuery)19 SQLException (java.sql.SQLException)12 List (java.util.List)11 TestForIssue (org.hibernate.testing.TestForIssue)11 Connection (java.sql.Connection)6 Statement (java.sql.Statement)6 EntityTransaction (javax.persistence.EntityTransaction)6 JPAEntityManager (org.datanucleus.api.jpa.JPAEntityManager)6 Person (org.datanucleus.samples.annotations.models.company.Person)6 ManagedConnection (org.datanucleus.store.connection.ManagedConnection)6 RDBMSStoreManager (org.datanucleus.store.rdbms.RDBMSStoreManager)6 DatastoreAdapter (org.datanucleus.store.rdbms.adapter.DatastoreAdapter)6 Parameter (javax.persistence.Parameter)5 Query (javax.persistence.Query)5 UserTransaction (javax.transaction.UserTransaction)5 QueryingTest (org.jpwh.test.querying.QueryingTest)5 Test (org.testng.annotations.Test)5