How can we call a stored procedure using Hibernate or JPA?
Here is the complete Solution to call a Stored Procedure with Just IN parameters ---
1) Create the Stored Procedure to act on a Table or a Set of Tables:
CREATE OR REPLACE procedure insertHouseHello (
house_date in timestamp,
house_name in varchar2,
house_number in number,
house_value in float)
is
begin
insert into House("HOUSE_DATE","HOUSE_NAME","HOUSE_NUMBER","HOUSE_VALUE")
values ( house_date, house_name,house_number,house_value);
commit;
end;
2) Execute the Stored Procedure from SQL Prompt to check the input. When You call the procedure from Java/Hibernate also You should see the similar result:
exec insertHouseHello(sysdate,'one',123,104);
3) In the Java Code:
log.info("Now trying to call the Stored Procedure*****************");
Query exQuery = session.createSQLQuery("CALL " +
"insertHouseHello(:timestmp,:hname,:hno,:hvalue)");
exQuery.setParameter("timestmp",
new java.sql.Timestamp(Calendar.getInstance().getTime().getTime()));
exQuery.setParameter("hname", 34);
exQuery.setParameter("hno", 212);
exQuery.setParameter("hvalue", 12);
int exRows = exQuery.executeUpdate();
log.info("Executed Rows from Stored Procedure****************"+exRows);
4) Now check the result in the Table, that should get updated accordingly: