How to call an Oracle function from Hibernate with a return parameter?

后端 未结 5 1626
你的背包
你的背包 2020-11-29 05:47

My question is very much like Getting the return value of a PL/SQL function via Hibernate

I have a function which does some modifications internally and it returns a

5条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 05:56

    Hibernate Session provides a doWork() method that gives you direct access to java.sql.Connection. You can then create and use java.sql.CallableStatement to execute your function:

    session.doWork(new Work() {
      public void execute(Connection connection) throws SQLException {
        CallableStatement call = connection.prepareCall("{ ? = call MYSCHEMA.MYFUNC(?,?) }");
        call.registerOutParameter( 1, Types.INTEGER ); // or whatever it is
        call.setLong(2, id);
        call.setLong(3, transId);
        call.execute();
        int result = call.getInt(1); // propagate this back to enclosing class
      }
    });
    

提交回复
热议问题