CREATE OR REPLACE PACKAGE BODY Pkg_name
IS
PROCEDURE proceure_Name (
Id IN Long,
startDate IN DATE,
EndDate IN DATE,
Table1 OUT Table1_Name%ROWTYPE,
Table2 OUT Table2_Name%ROWTYPE,
errormsg OUT VARCHAR2)
IS
Record another_Table%ROWTYPE;
我尝试在休眠中调用此过程,如下所示:
Long id = 111111111111111L;
Date startDate = setDateTime("08/31/2023 00:00:00");
Date endDate = setDateTime("09/01/2018 00:00:00"); // declaring stored Procedure
StoredProcedureQuery query = this.em.createStoredProcedureQuery("package_name.procedure_name"); // input parameters
query.registerStoredProcedureParameter("Id", Long.class, ParameterMode.IN);
query.setParameter("in_regEntId", id);
query.registerStoredProcedureParameter("startDate", Date.class, ParameterMode.IN);
query.setParameter("in_chPeriodStartDate", startDate);
query.registerStoredProcedureParameter("EndDate", Date.class, ParameterMode.IN);
query.setParameter("in_chPeriodEndDate", endDate); // register output Parameters
query.registerStoredProcedureParameter("Table1", Table1_name.class, ParameterMode.OUT);
query.registerStoredProcedureParameter("Table2", Table2_name.class, ParameterMode.OUT);
query.registerStoredProcedureParameter("errormsg", String.class, ParameterMode.OUT); // executing the stored Procedure
query.execute(); // output
问题:
因此,在注册输出参数时,我收到错误,因为类型 Table1_name 无效,
我不确定我应该在那里给出什么类型。
Nhibernate 不支持从存储过程返回复杂的结构,如
%rowtype
。
将存储过程更改为返回 SYS_REFCUSOR
。
例如:
PROCEDURE procedure_Name (
Id IN Long,
startDate IN DATE,
EndDate IN DATE,
Table1 OUT SYS_REFCURSOR,
Table2 OUT SYS_REFCURSOR,
errormsg OUT VARCHAR2
)
IS
BEGIN
//etc....
END;
然后将您的
nhibernate code
更新为类似的内容:
Long id = 111111111111111L;
Date startDate = setDateTime("08/31/2023 00:00:00");
Date endDate = setDateTime("09/01/2018 00:00:00");
StoredProcedureQuery query = this.em.createStoredProcedureQuery("package_name.procedure_name");
query.registerStoredProcedureParameter("Id", Long.class, ParameterMode.IN);
query.setParameter("Id", id);
query.registerStoredProcedureParameter("startDate", Date.class, ParameterMode.IN);
query.setParameter("startDate", startDate);
query.registerStoredProcedureParameter("EndDate", Date.class, ParameterMode.IN);
query.setParameter("EndDate", endDate);
query.registerStoredProcedureParameter("Table1", Class.forName("oracle.jdbc.OracleTypes").getField("CURSOR").get(null).getClass(), ParameterMode.REF_CURSOR);
query.registerStoredProcedureParameter("Table2", Class.forName("oracle.jdbc.OracleTypes").getField("CURSOR").get(null).getClass(), ParameterMode.REF_CURSOR);
query.registerStoredProcedureParameter("errormsg", String.class, ParameterMode.OUT);
query.execute();
// read output parameters
List<Object[]> resultsOfTable1 = query.getResultList();
List<Object[]> resultsOfTable2 = query.getResultList();
String error= query.getOutputParameterValue("errormsg");