休眠文档中的“使用存储过程的规则/限制”指出
“该过程必须返回一个结果集。请注意,由于这些服务器可以返回多个结果集和更新计数,因此 Hibernate 将迭代结果并将作为结果集的第一个结果作为其返回值。其他所有内容都将被丢弃。”
(参考:http ://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html#sp_query )
如前所述,您的案例中的第二个结果集被忽略。
您需要使用 jdbc 来获取两个结果集。您可以为此创建单独的类,或者,hibernate 通过其会话的“doWork”和“doReturningWork”方法为您提供执行传统 jdbc 操作的方法......
一个简单的例子可能是:
List<Object> res = session.doReturningWork(new ReturningWork<List<Object> /*objectType returned*/>() {
@Override
/* or object type you need to return to process*/
public List<Object> execute(Connection conn) throws SQLException
{
CallableStatement cstmt = conn.prepareCall("CALL YOUR_PROCEDURE");
//Result list that would return ALL rows of ALL result sets
List<Object> result = new ArrayList<Object>();
try
{
cstmt.execute();
ResultSet rs = cstmt.getResultSet(); // First resultset
while (rs.next()) {//Read items/rows of first resultset
// .
// Process rows of first resultset
result.add(obj); // add items of resultset 1 to the returning list object
}
cstmt.getMoreResults(); // Moves to this Statement object's next result, returns true if it is a ResultSet object
rs = cstmt.getResultSet(); // Second resultset
while (rs.next()) {
// .
// Process rows of second resultset
result.add(obj); // add items of resultset 2 to the returning list object
}
rs.close();
}
finally
{cstmt.close();}
return result; // this should contain All rows or objects you need for further processing
}
});