从具有多个引用游标的Hibernate调用存储过程

时间:2018-09-13 04:55:53

标签: java spring hibernate jpa stored-procedures

我正在尝试从存储过程中检索值。该存储过程有两个引用游标。下面是SP:

create or replace PROCEDURE "EMP_JOB" (
    p_job           VARCHAR2,
    p_emp_refcur    IN OUT SYS_REFCURSOR,
    p_sal_refcur    IN OUT SYS_REFCURSOR
)
IS
BEGIN
    OPEN p_emp_refcur FOR 
    SELECT empno, ename 
    FROM emp 
    WHERE job = p_job;

    OPEN p_sal_refcur FOR 
    SELECT sal 
    FROM emp 
    WHERE job = p_job;
END;

这是我的Java代码:

StoredProcedureQuery query = entityManager.createStoredProcedureQuery("EMP_JOB")
                .registerStoredProcedureParameter(1, String.class, ParameterMode.IN)
                .registerStoredProcedureParameter(2,  Class.class, ParameterMode.REF_CURSOR)
                .registerStoredProcedureParameter(3,  Class.class, ParameterMode.REF_CURSOR)        
                .setParameter(1, "CLERK");

query.execute();
Iterator queryIterator = query.getResultList().iterator();
ArrayList<Object> vinArray= new ArrayList<Object>();

while(queryIterator.hasNext()){
     Object st= (Object)queryIterator.next();
     vinArray.add(st);
}

我可以检索参数2(p_emp_refcur IN OUT SYS_REFCURSOR)的输出。

如何获取参数3(p_sal_refcur IN OUT SYS_REFCURSOR)的输出。

任何帮助将不胜感激。

预先感谢

2 个答案:

答案 0 :(得分:1)

看来还是不行。但是,这是通过从 Hibernate EntityManager 获取连接然后使用 CallableStatement 的解决方法。


// Session = org.hibernate.Session
// entityManager = javax.persistence.EntityManager

Session session = entityManager.unwrap(Session.class);
session.doWork(new Work() {

    @Override
    public void execute(Connection con) throws SQLException {
        // do something useful
        try (CallableStatement cs = con.prepareCall("{CALL TEST_PACKAGE.GETCURSORS(?,?,?)}")) {
            cs.setInt(1, 1);
            cs.registerOutParameter(2, OracleTypes.CURSOR);
            cs.registerOutParameter(3, OracleTypes.CURSOR);
            cs.execute();
            ResultSet rs = (ResultSet) cs.getObject(2);
            ResultSet rs1 = (ResultSet) cs.getObject(3);
            while (rs.next()) {
                int a = rs.getInt(1);
                System.out.println(a);
            }
            while (rs1.next()) {
                int b = rs1.getInt(1);
                System.out.println(b);
            }
        }
    }
});

答案 1 :(得分:0)

问题已经提交给Hibernate团队了:

https://hibernate.atlassian.net/browse/HHH-12596