这是我的存储过程
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();
在列表中,我只得到用户对象..关于我的id为1的enum_master行
ID为1的P.S enum_master行在DB中
谢谢.
解决方法
在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 } });