Search in sources :

Example 26 with ProcedureCall

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

the class StoredProcedureTest method testInParametersNotSet.

@Test
public void testInParametersNotSet() {
    Session session = openSession();
    session.beginTransaction();
    // since the procedure does not define defaults for parameters this should result in SQLExceptions on
    // execution
    {
        ProcedureCall query = session.createStoredProcedureCall("findUserRange");
        query.registerParameter(1, Integer.class, ParameterMode.IN);
        query.registerParameter(2, Integer.class, ParameterMode.IN).bindValue(2);
        try {
            query.getOutputs();
            fail("Expecting failure due to missing parameter bind");
        } catch (JDBCException expected) {
        }
    }
    // H2 does not support named parameters
    // {
    // ProcedureCall query = session.createStoredProcedureCall( "findUserRange" );
    // query.registerParameter( "start", Integer.class, ParameterMode.IN );
    // query.registerParameter( "end", Integer.class, ParameterMode.IN ).bindValue( 2 );
    // try {
    // query.getOutputs();
    // fail( "Expecting failure due to missing parameter bind" );
    // }
    // catch (JDBCException expected) {
    // }
    // }
    session.getTransaction().commit();
    session.close();
}
Also used : ProcedureCall(org.hibernate.procedure.ProcedureCall) JDBCException(org.hibernate.JDBCException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 27 with ProcedureCall

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

the class StoredProcedureTest method testInParametersByName.

// A warning should be logged if database metadata indicates named parameters are not supported.
@Test
public void testInParametersByName() {
    Session session = openSession();
    session.beginTransaction();
    ProcedureCall query = session.createStoredProcedureCall("findUserRange");
    query.registerParameter("start", Integer.class, ParameterMode.IN).bindValue(1);
    query.registerParameter("end", Integer.class, ParameterMode.IN).bindValue(2);
    ProcedureOutputs procedureResult = query.getOutputs();
    Output currentOutput = procedureResult.getCurrent();
    assertNotNull(currentOutput);
    ResultSetOutput resultSetReturn = assertTyping(ResultSetOutput.class, currentOutput);
    List results = resultSetReturn.getResultList();
    assertEquals(1, results.size());
    Object result = results.get(0);
    assertTyping(Object[].class, result);
    Integer id = (Integer) ((Object[]) result)[0];
    String name = (String) ((Object[]) result)[1];
    assertEquals(1, (int) id);
    assertEquals("User 1", name);
    session.getTransaction().commit();
    session.close();
}
Also used : ResultSetOutput(org.hibernate.result.ResultSetOutput) ProcedureCall(org.hibernate.procedure.ProcedureCall) ProcedureOutputs(org.hibernate.procedure.ProcedureOutputs) ResultSetOutput(org.hibernate.result.ResultSetOutput) Output(org.hibernate.result.Output) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 28 with ProcedureCall

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

the class StoredProcedureTest method testGetResultListTuple.

@Test
public void testGetResultListTuple() {
    Session session = openSession();
    session.beginTransaction();
    ProcedureCall query = session.createStoredProcedureCall("findUsers");
    ProcedureOutputs procedureResult = query.getOutputs();
    Output currentOutput = procedureResult.getCurrent();
    assertNotNull(currentOutput);
    ResultSetOutput resultSetReturn = assertTyping(ResultSetOutput.class, currentOutput);
    List results = resultSetReturn.getResultList();
    assertEquals(3, results.size());
    for (Object result : results) {
        assertTyping(Object[].class, result);
        Integer id = (Integer) ((Object[]) result)[0];
        String name = (String) ((Object[]) result)[1];
        if (id.equals(1)) {
            assertEquals("Steve", name);
        } else if (id.equals(2)) {
            assertEquals("John", name);
        } else if (id.equals(3)) {
            assertEquals("Jane", name);
        } else {
            fail("Unexpected id value found [" + id + "]");
        }
    }
    session.getTransaction().commit();
    session.close();
}
Also used : ResultSetOutput(org.hibernate.result.ResultSetOutput) ProcedureCall(org.hibernate.procedure.ProcedureCall) ProcedureOutputs(org.hibernate.procedure.ProcedureOutputs) ResultSetOutput(org.hibernate.result.ResultSetOutput) Output(org.hibernate.result.Output) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

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