2

我研究了很多关于如何使用带有 In & OUT 参数的 hibernate 4 session 对象调用存储过程,但没有用。

终于得到了与“SessionImpl”相关的线索。使用这个对象,我们可以获得 Connection 对象。

使用此连接对象可以调用 prepareCall() 方法,该方法将支持过程的 IN/OUT 参数。

以下是一个示例片段。

        Map<String, String> returnMap = new HashMap<String, String>(); 
        Session session = sessionFactory.getCurrentSession();
        Connection connObj = ((SessionImpl)session).connection();
        CallableStatement connStmt = connObj.prepareCall("{ call SCHEMA.PKG_NAME.SP_NAME(?,?,?) }");
        connStmt.setLong(1, 123456);

        connStmt.registerOutParameter(2, java.sql.Types.VARCHAR);
        connStmt.registerOutParameter(3, java.sql.Types.VARCHAR);
        connStmt.execute();

        if(connStmt != null && connStmt.getString(2).equalsIgnoreCase("Y")) {
            returnMap.put("returnID", ExceptionConstants.SUCCESS_STATUS);
            returnMap.put("returnMessage", connStmt.getString(3));  
        } else {
            returnMap.put("returnID", ExceptionConstants.FAILED_STATUS);
            returnMap.put("returnMessage", connStmt.getString(3));
        }
4

0 回答 0