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

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

问题描述

我的问题非常类似于获取通过Hibernate的PL / SQL函数的返回值



我有一个函数在内部做一些修改,并返回一个值。 b
$ b

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

  protected Integer checkXXX(Long id ,long传递)
抛出异常{
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。



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

解决方案

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

  session.doWork(new Work (){
public void execute(Connection connection)throws SQLException {
CallableStatement call = connection.prepareCall({?= call MYSCHEMA.MYFUNC(?,?)});
call .registerOutParameter(1,Types.INTEGER); //或任何它是
call.setLong(2,id);
call.setLong(3,transId);
call.execute );
int result = call.getInt(1); //将它传回以包含类
}
});


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 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();
}

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 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天全站免登陆