简体   繁体   中英

Calling stored procedure using hibernate

I have a stored procedure defined in a package. The procedure accepts two parameters, one is a cursor:

create or replace PACKAGE TESTPACKAGE IS
TYPE STATUSCURSOR IS REF CURSOR;
PROCEDURE TestProcedure(cId IN VARCHAR2,
StatusCursonVal IN OUT STATUSCURSOR);
END;

I am calling the procedure like this:

String sql = "EXEC TESTPACKAGE.TestProcedure('testId')";
Query query = session.createSQLQuery(sql);
        List list = query.list();

It is returning the following error:

13:03:25,338 INFO [stdout] (default task-3) Hibernate: EXEC TESTPACKAGE .TestProcedure('testId')

13:03:25,749 WARN [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-3) SQL Error: 900, SQLState: 42000 13:03:25,750 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-3) ORA-00900: invalid SQL statement

And the exception states "Could not extract resultSet".

I have tried to execute the procedure in SQL Developer but it failed. It seems the problem is I am executing the procedure with only one parameter but it also has an IN OUT parameter.

Please help how can I invoke this procedure.

Regards, Anirban.

As per hibernate documentation , correct way to call a stored procedure is -

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");

For executing Oracle stored procedure with cursor refer following 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM