Search in sources :

Example 51 with StoredProcedureQuery

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

the class MySQLStoredProcedureTest method testStoredProcedureReturnValue.

@Test
public void testStoredProcedureReturnValue() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        // tag::sql-jpa-call-sp-no-out-mysql-example[]
        StoredProcedureQuery query = entityManager.createStoredProcedureQuery("sp_phones");
        query.registerStoredProcedureParameter(1, Long.class, ParameterMode.IN);
        query.setParameter(1, 1L);
        List<Object[]> personComments = query.getResultList();
        // end::sql-jpa-call-sp-no-out-mysql-example[]
        assertEquals(2, personComments.size());
    });
}
Also used : StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Test(org.junit.Test)

Example 52 with StoredProcedureQuery

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

the class OracleStoredProcedureTest method testStoredProcedureRefCursor.

@Test
public void testStoredProcedureRefCursor() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        // tag::sql-jpa-call-sp-ref-cursor-oracle-example[]
        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();
        // end::sql-jpa-call-sp-ref-cursor-oracle-example[]
        assertNotNull(postComments);
    });
}
Also used : StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Test(org.junit.Test)

Example 53 with StoredProcedureQuery

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

the class HANAStoredProcedureTest method testOutAndSysRefCursorAsOutParameter.

@Test
@TestForIssue(jiraKey = "HHH-12138")
public void testOutAndSysRefCursorAsOutParameter() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        StoredProcedureQuery function = entityManager.createNamedStoredProcedureQuery("outAndRefCursor");
        function.execute();
        Integer value = (Integer) function.getSingleResult();
        assertEquals(Integer.valueOf(1), value);
        assertEquals(Integer.valueOf(1), function.getOutputParameterValue(1));
        assertFalse(function.hasMoreResults());
    });
}
Also used : StoredProcedureQuery(javax.persistence.StoredProcedureQuery) NamedStoredProcedureQuery(javax.persistence.NamedStoredProcedureQuery) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 54 with StoredProcedureQuery

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

the class PostgreSQLStoredProcedureTest method testStoredProcedureOutParameter.

@Test
public void testStoredProcedureOutParameter() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        StoredProcedureQuery query = entityManager.createStoredProcedureQuery("sp_count_phones");
        query.registerStoredProcedureParameter("personId", Long.class, ParameterMode.IN);
        query.registerStoredProcedureParameter("phoneCount", Long.class, ParameterMode.OUT);
        query.setParameter("personId", 1L);
        query.execute();
        Long phoneCount = (Long) query.getOutputParameterValue("phoneCount");
        assertEquals(Long.valueOf(2), phoneCount);
    });
}
Also used : StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Test(org.junit.Test)

Example 55 with StoredProcedureQuery

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

the class PostgreSQLStoredProcedureTest method testSysRefCursorAsOutParameter.

@Test
@TestForIssue(jiraKey = "HHH-11863")
public void testSysRefCursorAsOutParameter() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        Long value = null;
        Session session = entityManager.unwrap(Session.class);
        try (ResultSet resultSet = session.doReturningWork(connection -> {
            CallableStatement function = null;
            try {
                function = connection.prepareCall("{ ? = call singleRefCursor() }");
                function.registerOutParameter(1, Types.REF_CURSOR);
                function.execute();
                return (ResultSet) function.getObject(1);
            } finally {
                if (function != null) {
                    function.close();
                }
            }
        })) {
            while (resultSet.next()) {
                value = resultSet.getLong(1);
            }
        } catch (Exception e) {
            fail(e.getMessage());
        }
        assertEquals(Long.valueOf(1), value);
        StoredProcedureQuery function = entityManager.createStoredProcedureQuery("singleRefCursor");
        function.registerStoredProcedureParameter(1, void.class, ParameterMode.REF_CURSOR);
        function.execute();
        assertFalse(function.hasMoreResults());
        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) CallableStatement(java.sql.CallableStatement) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) ResultSet(java.sql.ResultSet) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLException(java.sql.SQLException) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

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