Search in sources :

Example 6 with ProcedureCall

use of org.hibernate.procedure.ProcedureCall in project hibernate-orm by hibernate.

the class MySQLStoredProcedureTest method testHibernateProcedureCallOutParameter.

@Test
public void testHibernateProcedureCallOutParameter() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        // tag::sql-hibernate-call-sp-out-mysql-example[]
        Session session = entityManager.unwrap(Session.class);
        ProcedureCall call = session.createStoredProcedureCall("sp_count_phones");
        call.registerParameter("personId", Long.class, ParameterMode.IN).bindValue(1L);
        call.registerParameter("phoneCount", Long.class, ParameterMode.OUT);
        Long phoneCount = (Long) call.getOutputs().getOutputParameterValue("phoneCount");
        assertEquals(Long.valueOf(2), phoneCount);
    // end::sql-hibernate-call-sp-out-mysql-example[]
    });
}
Also used : ProcedureCall(org.hibernate.procedure.ProcedureCall) Session(org.hibernate.Session) Test(org.junit.Test)

Example 7 with ProcedureCall

use of org.hibernate.procedure.ProcedureCall in project hibernate-orm by hibernate.

the class OracleStoredProcedureTest method testHibernateProcedureCallRefCursor.

@Test
public void testHibernateProcedureCallRefCursor() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        // tag::sql-hibernate-call-sp-ref-cursor-oracle-example[]
        Session session = entityManager.unwrap(Session.class);
        ProcedureCall call = session.createStoredProcedureCall("sp_person_phones");
        call.registerParameter(1, Long.class, ParameterMode.IN).bindValue(1L);
        call.registerParameter(2, Class.class, ParameterMode.REF_CURSOR);
        Output output = call.getOutputs().getCurrent();
        List<Object[]> postComments = ((ResultSetOutput) output).getResultList();
        assertEquals(2, postComments.size());
    // end::sql-hibernate-call-sp-ref-cursor-oracle-example[]
    });
}
Also used : ResultSetOutput(org.hibernate.result.ResultSetOutput) ProcedureCall(org.hibernate.procedure.ProcedureCall) Output(org.hibernate.result.Output) ResultSetOutput(org.hibernate.result.ResultSetOutput) Session(org.hibernate.Session) Test(org.junit.Test)

Example 8 with ProcedureCall

use of org.hibernate.procedure.ProcedureCall in project hibernate-orm by hibernate.

the class HANAStoredProcedureTest method testHibernateProcedureCallRefCursor.

@Test
@TestForIssue(jiraKey = "HHH-12138")
public void testHibernateProcedureCallRefCursor() {
    EntityManager entityManager = createEntityManager();
    entityManager.getTransaction().begin();
    try {
        Session session = entityManager.unwrap(Session.class);
        ProcedureCall call = session.createStoredProcedureCall("sp_person_phones");
        call.registerParameter(1, Long.class, ParameterMode.IN).bindValue(1L);
        call.registerParameter(2, Class.class, ParameterMode.REF_CURSOR);
        Output output = call.getOutputs().getCurrent();
        List<Object[]> postComments = ((ResultSetOutput) output).getResultList();
        assertEquals(2, postComments.size());
    } finally {
        entityManager.getTransaction().rollback();
        entityManager.close();
    }
}
Also used : ResultSetOutput(org.hibernate.result.ResultSetOutput) EntityManager(javax.persistence.EntityManager) ProcedureCall(org.hibernate.procedure.ProcedureCall) Output(org.hibernate.result.Output) ResultSetOutput(org.hibernate.result.ResultSetOutput) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 9 with ProcedureCall

use of org.hibernate.procedure.ProcedureCall in project hibernate-orm by hibernate.

the class StoredProcedureApiTests method testParameterBindTypeMismatch.

@Test
public void testParameterBindTypeMismatch() {
    inTransaction(session -> {
        try {
            final ProcedureCall call1 = session.createStoredProcedureCall("test");
            call1.registerStoredProcedureParameter(1, Integer.class, ParameterMode.IN);
            call1.setParameter(1, new Date());
            fail("expecting failure");
        } catch (IllegalArgumentException expected) {
        }
    });
}
Also used : ProcedureCall(org.hibernate.procedure.ProcedureCall) Date(java.util.Date) Test(org.junit.Test) StoredProcedureResultSetMappingTest(org.hibernate.test.sql.storedproc.StoredProcedureResultSetMappingTest)

Example 10 with ProcedureCall

use of org.hibernate.procedure.ProcedureCall in project hibernate-orm by hibernate.

the class StoredProcedureApiTests method parameterValueAccess.

@Test
public void parameterValueAccess() {
    inTransaction(session -> {
        final ProcedureCall call = session.createStoredProcedureCall("test");
        call.registerStoredProcedureParameter(1, Integer.class, ParameterMode.IN);
        call.registerStoredProcedureParameter(2, String.class, ParameterMode.OUT);
        call.setParameter(1, 1);
        call.getParameterValue(1);
    });
}
Also used : ProcedureCall(org.hibernate.procedure.ProcedureCall) Test(org.junit.Test) StoredProcedureResultSetMappingTest(org.hibernate.test.sql.storedproc.StoredProcedureResultSetMappingTest)

Aggregations

ProcedureCall (org.hibernate.procedure.ProcedureCall)28 Test (org.junit.Test)24 Session (org.hibernate.Session)18 ResultSetOutput (org.hibernate.result.ResultSetOutput)16 Output (org.hibernate.result.Output)14 ProcedureOutputs (org.hibernate.procedure.ProcedureOutputs)11 EntityManager (javax.persistence.EntityManager)4 List (java.util.List)3 StoredProcedureResultSetMappingTest (org.hibernate.test.sql.storedproc.StoredProcedureResultSetMappingTest)3 JDBCException (org.hibernate.JDBCException)2 ProcedureCallImpl (org.hibernate.procedure.internal.ProcedureCallImpl)2 DaoException (gov.ca.cwds.data.DaoException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Parameter (javax.persistence.Parameter)1 SessionFactory (org.hibernate.SessionFactory)1 Configuration (org.hibernate.cfg.Configuration)1 ProcedureCallMemento (org.hibernate.procedure.ProcedureCallMemento)1 QueryParameter (org.hibernate.query.QueryParameter)1 TestForIssue (org.hibernate.testing.TestForIssue)1