如何使用返回参数从 Hibernate 调用 Oracle 函数? [英] How to call an Oracle function from Hibernate with a return parameter?

查看:37
本文介绍了如何使用返回参数从 Hibernate 调用 Oracle 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很像 GettingPL/SQL 函数通过 Hibernate 的返回值

我有一个函数可以在内部进行一些修改并返回一个值.

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

最初的想法是做这样的事情:

The original idea was to do something like this:

protected Integer checkXXX(Long id, Long transId)
        throws Exception {
    final String sql = "SELECT MYSCHEMA.MYFUNC(" + id + ", "
            + transId + ") FROM DUAL";
    final BigDecimal nr = (BigDecimal) this.getHibernateTemplate()
            .getSessionFactory().getCurrentSession().createSQLQuery(sql)
            .uniqueResult();
    return nr.intValue();
}

不幸的是,这不适用于 Oracle.执行此类操作的推荐方法是什么?

Unfortunately this doesn't work with Oracle. What is the recommended way to do something like this?

有没有办法从我的语句中提取声明的变量?

Is there a way to extract declared variables from within my statement?

推荐答案

Hibernate Session 提供了一个 doWork() 方法,可让您直接访问 java.sql.Connection.然后您可以创建和使用 java.sql.CallableStatement 来执行你的函数:

Hibernate Session provides a doWork() method that gives you direct access to java.sql.Connection. You can then create and use java.sql.CallableStatement to execute your function:

session.doWork(new Work() {
  public void execute(Connection connection) throws SQLException {
    CallableStatement call = connection.prepareCall("{ ? = call MYSCHEMA.MYFUNC(?,?) }");
    call.registerOutParameter( 1, Types.INTEGER ); // or whatever it is
    call.setLong(2, id);
    call.setLong(3, transId);
    call.execute();
    int result = call.getInt(1); // propagate this back to enclosing class
  }
});

这篇关于如何使用返回参数从 Hibernate 调用 Oracle 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆