当我从hibernate调用存储过程时。结果集只返回一行,但在数据库表中只有四行数据。
这里我附上了来自db。
的表和数据的详细信息表名:TBL_EMPLOYEES
**Procedure :**
create or replace PROCEDURE "SP_GET_EMPLOYEES" (
ecur OUT sys_refcursor)
AS
BEGIN
OPEN ecur FOR
SELECT *FROM TBL_EMPLOYEES;
END;
Java代码(在Hibernate中调用Oracle存储过程)
Session session = HibernateSessionManager.getSessionFactory()
.openSession();
session.beginTransaction();
try {
session.doWork(new Work() {
public void execute(Connection connection) {
CallableStatement call;
try {
String aProcedureName="SP_GET_EMPLOYEES";
String functionCall = "{call " + aProcedureName + "(?)}";
call = connection.prepareCall( functionCall );
call.registerOutParameter(1, OracleTypes.CURSOR );
call.execute();
ResultSet rs=(ResultSet) call.getObject(1);
if(rs.next()){
System.out.println(rs.getString(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// propagate this back to enclosing class
}
});
} catch (Exception e) {
e.printStackTrace();
} finally {
session.getTransaction().commit();
}
O / P: 苏里亚
请帮我解决此问题。或者从hibernate到oracle存储过程的任何其他方法。
答案 0 :(得分:1)
在此处更改您的代码:
OLD:
if(rs.next()){
System.out.println(rs.getString(1));
}
新:
while(rs.next()){
System.out.println(rs.getString(1));
}
这是因为你没有做循环,你只是在第一次和第一次检查条件。
答案 1 :(得分:0)
您只获得单曲
使用
ResultSet rs = call.getResultSet();
while(rs.next())
{
sysout(rs.getString(1));
}
您实际上只检索了一条记录。
答案 2 :(得分:0)
感谢您的代码。
我做了一些细微的改动,并且工作正常。
过程脚本:
create or replace PROCEDURE GET_EMPLOYEES_1 (
ecur OUT sys_refcursor)
AS
BEGIN
OPEN ecur FOR
SELECT * FROM EMPLOYEE;
END;
休眠:
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
try {
session.doWork(new Work() {
public void execute(Connection connection) {
CallableStatement call;
try {
String aProcedureName = "GET_EMPLOYEES_1";
String functionCall = "{call " + aProcedureName + "(?)}";
call = connection.prepareCall(functionCall);
call.registerOutParameter(1, OracleTypesHelper.INSTANCE.getOracleCursorTypeSqlType());
call.execute();
ResultSet rs = (ResultSet) call.getObject(1);
if (rs.next()) {
logger.info("ID:" + rs.getString(1));
logger.info("status" + rs.getString(3));
logger.info("Location" + rs.getString(4));
logger.info("First name" + rs.getString(5));
logger.info("Last name" + rs.getString(6));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// propagate this back to enclosing class
}
});
} catch (Exception e) {
e.printStackTrace();
} finally {
session.getTransaction().commit();
}