How can we call a stored procedure with Hibernate and JPA?

后端 未结 7 1381
时光取名叫无心
时光取名叫无心 2020-12-15 06:34

How can we call a stored procedure using Hibernate or JPA?

7条回答
  •  心在旅途
    2020-12-15 07:29

    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:

提交回复
热议问题