maddy
maddy

Reputation: 358

Oracle Stored Procedure Execution in Hibernate Query

I have a procedure like this :

create or replace PROCEDURE Greeting1 IS sqlstmt varchar2(400);
BEGIN
   sqlstmt:= 'select * from Object_set';
   EXECUTE immediate sqlstmt;
END;

I want to execute it in java code like this:

List<String> result = null;
Query query = getEm().unwrap(Session.class).createSQLQuery("CALL Greeting1()");
    if(query.list() != null) {
         result = query.list();
    }
return result;

But it gives error while getting query.list(). Actually it returns null in Query Objects. How should I get the proper result list. Please suggest me.

Upvotes: 2

Views: 1170

Answers (2)

Baptiste Beauvais
Baptiste Beauvais

Reputation: 2086

I don't really know stored procedure with Oracle but JPA set an interface to execute stored procedure, you should read this: Calling Stored procedure with Hibernate.

This take care of the case where you use IN, OUT and IN OUT parameters, otherwise for stored function you will have to use jdbc API instead.

Hope it helped.

Upvotes: 2

Angelo Immediata
Angelo Immediata

Reputation: 6954

I guess it's not possible to use the query.list(); method What I usually do when I need to inovke stored procedure in a hibernate+spring environment is something like this:

//sessionFactory objext is autowired in this DAO
Session hibSession = sessionFactory.getCurrentSession();
hibSession.doWork(new Work()
{
    @Override
    public void execute(Connection sqlConnection) throws SQLException
    {
        //Here I call the stored procedure and valorize my java POJOS if needed
    }
});

I hope it's useful

Angelo

Upvotes: 0

Related Questions