如何使用返回参数从Hibernate调用Oracle函数? [英] How to call an Oracle function from Hibernate with a return parameter?
问题描述
我的问题非常类似于获取通过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屋!