hibernate 5 oracleストレージ・プロシージャと関数を呼び出す
新しいストアド・プロシージャと関数:
1,戻り値test_なしproc_no_result:
2,戻り値test_があるproc_has_result :
3,関数test_func :
呼び出しコードは次のとおりです.
1,戻り値test_なしproc_no_result:
create or replace procedure test_proc_no_result(in_name varchar2) is
begin
insert into test_table .....;
commit;
end test_proc_no_result;
2,戻り値test_があるproc_has_result :
create or replace procedure test_proc_has_result(in_name in varchar2,out_name out varchar2) is
begin
out_name := 'hello ' || in_name;
end test_proc_has_result;
3,関数test_func :
create or replace function test_func(in_name varchar2)
return varchar2 is
out_name varchar2(200) :='' ;
begin
out_name := 'hello ' || in_name;
return out_name;
end test_func;
呼び出しコードは次のとおりです.
@Autowired
private SessionFactory sessionFactory;
//
public void callProcNoResult(String name){
String sql="{call test_proc_no_result(?)}";
sessionFactory.getCurrentSession().createNativeQuery(sql).setParameter(1, name).executeUpdate();
}
//
public Object callProcHasResult(String name) throws SQLException{
ProcedureCall call=sessionFactory.getCurrentSession().createStoredProcedureCall("test_proc_has_result");
// ,
call.registerParameter(1, String.class, ParameterMode.IN).bindValue(name);
call.registerParameter(2, String.class, ParameterMode.OUT);
return call.getOutputs().getOutputParameterValue(2);
}
//
public Object callFunction(String name){
String sql="select test_func(?) from dual";
return sessionFactory.getCurrentSession().createNativeQuery(sql).setParameter(1, name).uniqueResult();
}