在Hibernate 4.2中,我尝试调用返回游标的存储过程(我使用Oracle DB)。
程序如下:
PROCEDURE SYS_columnNames ( ownerIn in sys.all_tab_cols.owner%type,
tableName in sys.all_tab_cols.table_name%type,
resultCur out curRef )
is
begin
open resultCur for
select column_name from sys.all_tab_cols
where owner = ownerIn
and Upper(Trim(table_name)) = tableName
order by column_name;
exception
when others then
null;
end SYS_columnNames ;
我的Java代码:
Session session = null;
try
{
SessionFactory sessionFactory = sessionFactoryManager.getCurrentSession();
session = sessionFactory.openSession();
return session.doReturningWork( new ReturningWork< T >()
{
@SuppressWarnings( "unchecked" )
@Override
public T execute( Connection aConnection ) throws SQLException
{
CallableStatement callstm = null;
try
{
String functionCall = "{call " + aProcedureName + "(:owner, :tableName, :rescur)}";
callstm = aConnection.prepareCall( functionCall );
callstm.setString( "owner", "MYOWNER" );
callstm.setString( "tableName", "USER_TABLE" );
callstm.registerOutParameter( "rescur", OracleTypes.CURSOR );
callstm.execute();
return (T)callstm.getObject( 3 );
}
finally
{
closeQuietly( callstm );
}
}
} );
}
finally
{
closeQuietly( session );
}
但是这给了我一个错误:
org.hibernate.exception.GenericJDBCException: error executing work
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.coordinateWork(JdbcCoordinatorImpl.java:289)
at org.hibernate.internal.SessionImpl.doWork(SessionImpl.java:1953)
at org.hibernate.internal.SessionImpl.doReturningWork(SessionImpl.java:1949)
(...)
Caused by: java.sql.SQLException: operation not allowed: Ordinal binding and Named binding cannot be combined!
at oracle.jdbc.driver.OracleCallableStatement.getObject(OracleCallableStatement.java:2027)
at oracle.jdbc.driver.OracleCallableStatementWrapper.getObject(OracleCallableStatementWrapper.java:816)
at org.hibernate.jdbc.WorkExecutor.executeReturningWork(WorkExecutor.java:72)
at org.hibernate.internal.SessionImpl$3.accept(SessionImpl.java:1946)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.coordinateWork(JdbcCoordinatorImpl.java:284)
... 58 more
我完全不知道如何调用返回游标的存储过程。任何帮助将不胜感激。
答案 0 :(得分:1)
解决方法是使用'?'而不是变量名:
String functionCall = "{call " + aProcedureName + "(?, ?, ?)}";
callstm = aConnection.prepareCall( functionCall );
callstm.setString( 1, "MYOWNER" );
callstm.setString( 2, "USER_TABLE" );
callstm.registerOutParameter( 3, OracleTypes.CURSOR );
答案 1 :(得分:0)
问题出在以下陈述之一
callstm.registerOutParameter( "rescur", OracleTypes.CURSOR );
callstm.execute();
return (T)callstm.getObject( 3 );
在上面的代码中在第1行设置参数时;您在回取结果时使用命名参数绑定,而在使用顺序绑定,即第3行;如果您将第3行命名为绑定,如下所示; 那就可以了
return (T)callstm.getObject("rescur");
我使用了与您相同的代码;只需更正return语句,它就像一个魅力。
编码愉快!