Submitted by: krish_1911
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();
Above answer was rated as good by the following members:
mani_id