本文主要是介绍Hibernate 调用函数及存储过程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、函数
第一种方式:获得connection
Connection conn = session.connection();CallableStatement call = conn.prepareCall("{?=call getemp.getempc(?)}");call.registerOutParameter(1, OracleTypes.CURSOR);// 设置输出变量类型call.setInt(2, 10);call.executeQuery();ResultSet rs = (ResultSet) call.getObject(1);while (rs.next()) {System.out.println(rs.getString(3));}
对应函数包体:
create or replace package body getemp isfunction getempc(dno number) return sys_refcursoristype emp_cursor_type is ref cursor;emp_cursor emp_cursor_type;beginopen emp_cursor for select * from scott.emp where deptno=dno;return emp_cursor;end;
end getemp;
二、存储过程
对应的存储过程:
create or replace procedure getdeptbyid(d out sys_refcursor,dno number,num number )
is
beginopen d for select * from dept where deptno=dno and 1=num;
end;
第一种方式:获得connection
Connection conn = session.connection();CallableStatement call = conn.prepareCall("{call getdeptbyid(?,?,?)}");call.registerOutParameter(1, OracleTypes.CURSOR);// 设置输出变量类型call.setInt(2, 10);call.setInt(3, 1);call.executeQuery();ResultSet rs = (ResultSet) call.getObject(1);while (rs.next()) {System.out.println(rs.getString(2));}
这篇关于Hibernate 调用函数及存储过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!