我的问题很像通过 Hibernate 获取 PL/SQL 函数的返回值 https://stackoverflow.com/questions/1068974/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() http://docs.jboss.org/hibernate/stable/core/api/org/hibernate/Session.html#doWork(org.hibernate.jdbc.Work)方法让您可以直接访问java.sql.Connection http://java.sun.com/javase/6/docs/api/java/sql/Connection.html。然后您可以创建并使用java.sql.CallableStatement http://java.sun.com/javase/6/docs/api/java/sql/CallableStatement.html执行你的函数:
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
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)