Search in sources :

Example 6 with Output

use of org.hibernate.result.Output in project hibernate-orm by hibernate.

the class StoredProcedureTest method testGetSingleResultTuple.

@Test
public void testGetSingleResultTuple() {
    Session session = openSession();
    session.beginTransaction();
    ProcedureCall query = session.createStoredProcedureCall("findOneUser");
    ProcedureOutputs procedureResult = query.getOutputs();
    Output currentOutput = procedureResult.getCurrent();
    assertNotNull(currentOutput);
    ResultSetOutput resultSetReturn = assertTyping(ResultSetOutput.class, currentOutput);
    Object result = resultSetReturn.getSingleResult();
    assertTyping(Object[].class, result);
    String name = (String) ((Object[]) result)[1];
    assertEquals("Steve", 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) Session(org.hibernate.Session) Test(org.junit.Test)

Example 7 with Output

use of org.hibernate.result.Output in project hibernate-orm by hibernate.

the class MySQLStoredProcedureTest method testHibernateProcedureCallReturnValueParameter.

@Test
public void testHibernateProcedureCallReturnValueParameter() {
    EntityManager entityManager = createEntityManager();
    entityManager.getTransaction().begin();
    try {
        Session session = entityManager.unwrap(Session.class);
        ProcedureCall call = session.createStoredProcedureCall("sp_phones");
        call.registerParameter(1, Long.class, ParameterMode.IN).bindValue(1L);
        Output output = call.getOutputs().getCurrent();
        List<Object[]> personComments = ((ResultSetOutput) output).getResultList();
        assertEquals(2, personComments.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)

Example 8 with Output

use of org.hibernate.result.Output in project hibernate-orm by hibernate.

the class MySQLStoredProcedureTest method testHibernateProcedureCallReturnValueParameter.

@Test
public void testHibernateProcedureCallReturnValueParameter() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        // tag::sql-hibernate-call-sp-no-out-mysql-example[]
        Session session = entityManager.unwrap(Session.class);
        ProcedureCall call = session.createStoredProcedureCall("sp_phones");
        call.registerParameter(1, Long.class, ParameterMode.IN).bindValue(1L);
        Output output = call.getOutputs().getCurrent();
        List<Object[]> personComments = ((ResultSetOutput) output).getResultList();
        // end::sql-hibernate-call-sp-no-out-mysql-example[]
        assertEquals(2, personComments.size());
    });
}
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 9 with Output

use of org.hibernate.result.Output in project hibernate-orm by hibernate.

the class ResultMappingTest method testResultClass.

@Test
public void testResultClass() {
    inTransaction(session -> {
        final ProcedureCall call = session.createStoredProcedureCall("findOneUser", H2ProcTesting.MyEntity.class);
        final ProcedureOutputs procedureResult = call.getOutputs();
        final Output currentOutput = procedureResult.getCurrent();
        assertNotNull(currentOutput);
        final ResultSetOutput resultSetReturn = assertTyping(ResultSetOutput.class, currentOutput);
        final Object result = resultSetReturn.getSingleResult();
        assertTyping(H2ProcTesting.MyEntity.class, result);
        assertEquals("Steve", ((H2ProcTesting.MyEntity) result).name);
    });
}
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) Test(org.junit.Test)

Example 10 with Output

use of org.hibernate.result.Output in project hibernate-orm by hibernate.

the class ResultMappingTest method testMappingNoFields.

@Test
public void testMappingNoFields() {
    inTransaction(session -> {
        final ProcedureCall call = session.createStoredProcedureCall("findOneUser", "no-fields");
        final ProcedureOutputs procedureResult = call.getOutputs();
        final Output currentOutput = procedureResult.getCurrent();
        assertNotNull(currentOutput);
        final ResultSetOutput resultSetReturn = assertTyping(ResultSetOutput.class, currentOutput);
        final Object result = resultSetReturn.getSingleResult();
        assertTyping(H2ProcTesting.MyEntity.class, result);
        assertEquals("Steve", ((H2ProcTesting.MyEntity) result).name);
    });
}
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) Test(org.junit.Test)

Aggregations

ProcedureCall (org.hibernate.procedure.ProcedureCall)14 Output (org.hibernate.result.Output)14 ResultSetOutput (org.hibernate.result.ResultSetOutput)14 Test (org.junit.Test)14 Session (org.hibernate.Session)10 ProcedureOutputs (org.hibernate.procedure.ProcedureOutputs)9 List (java.util.List)3 EntityManager (javax.persistence.EntityManager)3 TestForIssue (org.hibernate.testing.TestForIssue)1