这是我的存储过程
Create PROCEDURE [dbo].getUserAndEnum
AS
BEGIN
select * from user_master where id =1
select * from enum_master where id = 1
End
用hibernate编写
Session session = HibernateFactory.getSessionFactory().openSession();
Transaction tr = session.beginTransaction();
SQLQuery qr=session.createSQLQuery("getUserAndEnum");
List list = qr.list();
在列表中,我只获取用户对象..我的enum_master行,ID为1
DB
中存在ID为1的P.Senum_master行感谢。
答案 0 :(得分:4)
'在hibernate文档中使用存储过程的规则/限制'表明
“该过程必须返回结果集。请注意,由于这些服务器可以返回多个结果集和更新计数,因此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
}
});