我的问题很像Getting the return value of a PL/SQL function via Hibernate
我有一个函数,它在内部进行一些修改并返回一个值。
最初的想法是做这样的事情:
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。执行此类操作的推荐方法是什么?
有没有办法从我的语句中提取声明的变量?
最佳答案
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 ); // 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
}
});
关于java - 如何使用返回参数从 Hibernate 调用 Oracle 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1703351/