Stored procedure in hibernate

How to call stored procedure in mysql through hibernate?

Questions by bhupeshb

Showing Answers 1 - 9 of 9 Answers

As per sql-->

create a procedure in your sql by using the following code.
create or replace procedure first-procedure(x in number,y out number)
begin
y:=x*x;
end;
 write your sql connection code in hibernate configuration file. In your hibernate client application get the session obj. On the session object call the connection() method and get the connection. 
ex: Connection con=session.connection();
      CallableStatement cst=con.prepareCall("{call first-procedure(?,?)}");
      cst.registerOutParameter(2,Types.Integer);
      cst.setInt(1,20);
      cst.execute();
      int res=cst.getInt(2);
     s.o.p(res);
     ses.close();

  Was this answer useful?  Yes

As per sql-->

create a procedure in your sql by using the following code.
create or replace procedure first-procedure(x in number,y out number)
begin
y:=x*x;
end;
 write your sql connection code in hibernate configuration file.In your hibernate client application get the session obj.On the session object call the connection() method and get the connection. 
ex: Connection con=session.connection();
      CallableStatement cst=con.prepareCall("{call first-procedure(?,?)}");
      cst.registerOutParameter(2,Types.Integer);
      cst.setInt(1,20);
      cst.execute();
      int res=cst.getInt(2);
     s.o.p(res);
     ses.close();

  Was this answer useful?  Yes

krish_1911

  • Nov 17th, 2008
 

In order to call a stored procedure using the hibernate , define a named query for a persistent class mapping document.
Then call the named query from your java application .Example is given in the Hibernate Reference documentation as follows:-

First create a stored procedure in your database as,

CREATE OR REPLACE FUNCTION selectAllEmployments
RETURN SYS_REFCURSOR
AS
st_cursor SYS_REFCURSOR;
BEGIN
OPEN st_cursor FOR
SELECT EMPLOYEE, EMPLOYER,
STARTDATE, ENDDATE,
REGIONCODE, EID, VALUE, CURRENCY
FROM EMPLOYMENT;
RETURN st_cursor;
END;

Then in the mapping document create a named query as follows:-

<sql-query name="selectAllEmployees_SP" callable="true">
<return alias="emp" class="Employment">
<return-property name="employee" column="EMPLOYEE"/>
<return-property name="employer" column="EMPLOYER"/>
<return-property name="startDate" column="STARTDATE"/>
<return-property name="endDate" column="ENDDATE"/>
<reeturn-property name="regionCode" column="REGIONCODE"/>
<return-property name="id" column="EID"/>
<return-property name="salary">
<return-column name="VALUE"/>
<return-column name="CURRENCY"/>
</return-property>
</return>
{ ? = call selectAllEmployments() }
</sql-query>

Then, in your java code just call the named query as follows:-
List people = sess.getNamedQuery("selectAllEmployees_SP")
.setString("namePattern", namePattern)
.setMaxResults(50)
.list();

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