Execute Stored procedure in Hibernate

How to invoke a stored procedure in Hibernate and pass in a parameter?

I have



{ ? = call myservice.TEST_PROC3( ? ) }


While I can get stored proc output if there is no input to TEST_PROC3, e.g.

call myservice.TEST_PROC3( )

I don't know how to code in Java to pass in the parameter required in my case with:

call myservice.TEST_PROC3( ? )

Can anyone show me how to make such call in Java?

Thanks

Showing Answers 1 - 15 of 15 Answers

Connection con = null;
      

        try {
            con = session.connection();
            
            CallableStatement st = con
                    .prepareCall("{call your_sp(?,?)}");
            st.registerOutParameter(2, Types.INTEGER);
            st.setString(1, "some_Seq");
            
            st.executeUpdate();

  Was this answer useful?  Yes

By this process you can write hibernate procedure:

<sql-query name="selectAllEmployees_SP" callable="true">
<return alias="emp" class="employee">
   <return-property name="empid" column="EMP_ID"/>       

  <return-property name="name" column="EMP_NAME"/>       
  <return-property name="address" column="EMP_ADDRESS"/>
    { ? = call selectAllEmployees() }
</return>
</sql-query>

You can declare it as Named queiries in one of your mapping files and give unique name.

Call it in your code. Sample code is below.

SQLQuery sq = (SQLQuery) session.getNamedQuery("findSearchResultsListSP");

sq.addEntity("documentTypeCode", documentTypeCode);

List results = sq.list();

This should work.

All the best

jyothi_4

  • Feb 12th, 2011
 

We can execute stored procedures in hibernate using Native SQL.
                                    (OR)
Get the Connection object using Session object and then using Connection object create callable Statement

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions