Search in sources :

Example 6 with ResultSetOutput

use of org.hibernate.result.ResultSetOutput 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 7 with ResultSetOutput

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

the class MySQLStoredProcedureTest method testHibernateProcedureCallReturnValueParameter.

@Test
public void testHibernateProcedureCallReturnValueParameter() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        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());
    });
}
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 ResultSetOutput

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

the class StoredProcedureResultSetMappingTest method testPartialResults.

@Test
public void testPartialResults() {
    Configuration cfg = new Configuration().addAnnotatedClass(Employee.class).setProperty(AvailableSettings.HBM2DDL_AUTO, "create-drop");
    cfg.addAuxiliaryDatabaseObject(new ProcedureDefinition());
    SessionFactory sf = cfg.buildSessionFactory();
    try {
        Session session = sf.openSession();
        session.beginTransaction();
        ProcedureCall call = session.createStoredProcedureCall("allEmployeeNames", "id-fname-lname");
        ProcedureOutputs outputs = call.getOutputs();
        ResultSetOutput output = assertTyping(ResultSetOutput.class, outputs.getCurrent());
        assertEquals(3, output.getResultList().size());
        assertTyping(Employee.class, output.getResultList().get(0));
        session.getTransaction().commit();
        session.close();
    } finally {
        sf.close();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) ResultSetOutput(org.hibernate.result.ResultSetOutput) ProcedureCall(org.hibernate.procedure.ProcedureCall) Configuration(org.hibernate.cfg.Configuration) ProcedureOutputs(org.hibernate.procedure.ProcedureOutputs) Session(org.hibernate.Session) Test(org.junit.Test)

Example 9 with ResultSetOutput

use of org.hibernate.result.ResultSetOutput 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) Output(org.hibernate.result.Output) ResultSetOutput(org.hibernate.result.ResultSetOutput) List(java.util.List) AuxiliaryDatabaseObject(org.hibernate.boot.model.relational.AuxiliaryDatabaseObject) Session(org.hibernate.Session) Test(org.junit.Test)

Example 10 with ResultSetOutput

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

the class StoredProcedureTest method testInParametersByPosition.

@Test
public void testInParametersByPosition() {
    Session session = openSession();
    session.beginTransaction();
    ProcedureCall query = session.createStoredProcedureCall("findUserRange");
    query.registerParameter(1, Integer.class, ParameterMode.IN).bindValue(1);
    query.registerParameter(2, 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) Output(org.hibernate.result.Output) ResultSetOutput(org.hibernate.result.ResultSetOutput) List(java.util.List) AuxiliaryDatabaseObject(org.hibernate.boot.model.relational.AuxiliaryDatabaseObject) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Session (org.hibernate.Session)11 ProcedureCall (org.hibernate.procedure.ProcedureCall)11 ResultSetOutput (org.hibernate.result.ResultSetOutput)11 Test (org.junit.Test)11 Output (org.hibernate.result.Output)9 ProcedureOutputs (org.hibernate.procedure.ProcedureOutputs)7 AuxiliaryDatabaseObject (org.hibernate.boot.model.relational.AuxiliaryDatabaseObject)4 List (java.util.List)3 EntityManager (javax.persistence.EntityManager)2 SessionFactory (org.hibernate.SessionFactory)1 Configuration (org.hibernate.cfg.Configuration)1