How to call an Oracle function from Hibernate with a return parameter?

后端 未结 5 1613
你的背包
你的背包 2020-11-29 05:47

My question is very much like Getting the return value of a PL/SQL function via Hibernate

I have a function which does some modifications internally and it returns a

5条回答
  •  自闭症患者
    2020-11-29 06:10

    public static void getThroHibConnTest() throws Exception {
        logger.debug("UsersActiion.getThroHibConnTest() | BEG ");
        Transaction tx = null;
        Connection conn = null;
        CallableStatement cs = null;
        Session session = HibernateUtil.getInstance().getCurrentSession();
        try {
            tx = session.beginTransaction();
            conn = session.connection();
    
            System.out.println("Connection = "+conn);
            if (cs == null)
            {
                cs = 
                    conn.prepareCall("{ ?=call P_TEST.FN_GETSUM(?,?) }");
            }
            cs.clearParameters();
            cs.registerOutParameter(1,OracleTypes.INTEGER);
            cs.setInt(2,1);
            cs.setInt(3,2);
            cs.execute();
            int retInt=cs.getInt(1);
            tx.commit();            
        }catch (Exception ex) {  
            logger.error("UsersActiion.getThroHibConnTest() | ERROR | " , ex);  
            if (tx != null && tx.isActive()) {
                try {
                    // Second try catch as the rollback could fail as well
                    tx.rollback();
                } catch (HibernateException e1) {
                    logger.debug("Error rolling back transaction");
                }
                // throw again the first exception
                throw ex;
            }
        }finally{
            try {
                if (cs != null) {
                    cs.close();
                    cs = null;
                }
                if(conn!=null)conn.close();
    
            } catch (Exception ex){;}
        }
        logger.debug("UsersActiion.getThroHibConnTest() | END ");
    }
    

提交回复
热议问题