This is how to get the result in a Java class (hopefully you can port it to Hibernate using this question as an exemplar):
Oracle Setup:
CREATE FUNCTION get_ref_cursor RETURN SYS_REFCURSOR
IS
out_cursor SYS_REFCURSOR;
BEGIN
OPEN out_cursor FOR
SELECT 123 AS col1 FROM DUAL UNION ALL
SELECT 456 FROM DUAL UNION ALL
SELECT 789 FROM DUAL;
RETURN out_cursor;
END;
/
Java:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;
public class GetRefCursorFromFunction
{
public static void main( final String[] args ){
try{
Class.forName( "oracle.jdbc.OracleDriver" );
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl",
"USERNAME",
"PASSWORD"
);
OracleCallableStatement st =
(OracleCallableStatement) con.prepareCall( "BEGIN :1 := get_Ref_Cursor(); END;" );
st.registerOutParameter( 1, OracleTypes.CURSOR );
System.out.println( st.execute() );
ResultSet rs = st.getCursor( 1 );
while ( rs.next() )
{
System.out.println( rs.getInt( 1 ) );
}
st.close();
con.close();
} catch (ClassNotFoundException | SQLException ex) {
System.out.println( ex.getMessage() );
ex.printStackTrace();
}
}
}
(Note: this assumes you are using Oracle's driver to connect to the database.)
Output:
123
456
789
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…