1

Below is the stored procedure:

create or replace procedure 
proc_emp_name(v_emp out emp.emp_name%TYPE, v_empid in emp.emp_id%TYPE)
is
begin
select emp_name into v_emp from emp where emp_id = v_empid;
dbms_output.put_line('Emp Name: ' || v_emp);
dbms_output.put_line('Procedure created successfully!!!');
end;

I want to invoke this using Native SQL, followed this link but not sure how to retrieve the OUT parameter from the Procedure.

http://www.mkyong.com/hibernate/how-to-call-store-procedure-in-hibernate/

Kindly let me know the simplest way to invoke the procedure and get the results out of it.

EDIT

As suggested, checking the docs, I modified the Proc having first parameter as a SYS_REFCURSOR as follows:

create or replace procedure
proc_empname_refcursor(v_empname OUT SYS_REFCURSOR, v_deptid in emp.emp_id%type)
is 
begin
open v_empname for select * from dept where dept_id = v_deptid;
end;

I am able to invoke it using NamedQuery but I don't want to add anything in the mapping files because of some other restrictions. I tried the below code for invoking the proc without using NamedQuery but it did not worked out:

Query query = session.createSQLQuery(
                "CALL proc_empname_refcursor(?, :deptId)")
                .addEntity(Dept.class)
                .setParameter("deptId", new Integer(2));

            List<Dept> departments = query.list();
            for(int i=0; i<departments.size(); i++){
                Dept department = (Dept)departments.get(i);
                System.out.println("Dept Id: " + department.getDeptId());
                System.out.println("Dept Name: " + department.getDeptName());
            }

I am getting the exception:

org.hibernate.QueryException: Expected positional parameter count: 1, actual parameters: [] [CALL proc_empname_refcursor(?, :deptId)]
at     org.hibernate.impl.AbstractQueryImpl.verifyParameters(AbstractQueryImpl.java:319)
at org.hibernate.impl.SQLQueryImpl.verifyParameters(SQLQueryImpl.java:201)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:145)
at com.jdbc.HibernateStartup.main(HibernateStartup.java:70)

Kindly let me know how to resolve this.

2 Answers 2

6

I've managed to get an out parameter from a stored procedure using the following code in Hibernate and MS SQL Server:

@Override
public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
    Connection connection = session.connection();
    CallableStatement callable = null;
    try {
        callable = connection.prepareCall("execute [procedure] ?");
        callable.registerOutParameter(1, Types.INTEGER);
        callable.execute();
        int id = callable.getInt(1);

        return id;
    } catch (SQLException e) {
        (...)
    } finally {
        (...)
    }
}
6
1

From Hibernate Docs:

You cannot use stored procedures with Hibernate unless you follow some procedure/function rules.

For Oracle, A function must return a result set. The first parameter of a procedure must be an OUT that returns a result set.

4
  • Thanks for the information. I am able to invoke the proc as named query by adding it into mapping files. But now the problem is I dont want to add it in mapping and use it as named-query, rather I want to invoke it using directly using createSQLQuery. I tried that but got an exception: org.hibernate.QueryException: Expected positional parameter count: 1, actual parameters: [] [CALL proc_empname_refcursor(?, :deptId)] I have updated my first post to provide what exactly I tried, kindly let me know how to resolve this exception.
    – user182944
    Commented Jun 28, 2013 at 6:30
  • Remove the '?'. it should be CALL proc_empname_refcursor(:deptId). see example here Commented Jun 29, 2013 at 22:55
  • If I remove the "?" then I am getting this exception: java.sql.SQLException: ORA-06553: PLS-306: wrong number or types of arguments in call to 'PROC_EMPNAME_REFCURSOR'
    – user182944
    Commented Jun 30, 2013 at 4:15
  • are you able to sort out the problem as I am also getting the same issue while calling a stored function via hibernate
    – Abhi
    Commented Jun 13, 2015 at 12:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.