使用jpa 2.0调用具有out参数的存储过程

时间:2014-10-29 19:57:16

标签: java hibernate jpa

以下是程序。

create or replace 
    PROCEDURE test ( upload_id_in in NUMBER, o in out NUMBER)
    is

    begin

    select  safety_id into o from case_smsw_master where safety_id=111722;

    end;

以下是调用该过程的代码。

           Query q = em.createNativeQuery("CALL test(?)");
            q.setParameter(1, Long.valueOf(tempCaseId));
            q.setHint("org.hibernate.callable", true);
            Object i=q.executeUpdate();//getResultList();//executeUpdate();

例外是result set meatadata not found。 可能是什么原因。

1 个答案:

答案 0 :(得分:0)

请尝试以下代码。

/**************************************************/
/* Call MYSQL Stored Procedure and MAP it to bean */
/**************************************************/
Query callStoredProcedure_MYSQL = session.createSQLQuery("CALL SP_MYSQL_HIBERNATE (:param1, :param2, :param3)").addEntity(User.class);
callStoredProcedure_MYSQL.setInteger("param1", 10);
callStoredProcedure_MYSQL.setInteger("param2", 10);
callStoredProcedure_MYSQL.setString("param3", "javaQuery");

/* callStoredProcedure_MSSQL.list() will execute stored procedure and return the value */
List userList = callStoredProcedure_MYSQL.list();
if (userList != null && !userList.isEmpty()) {
 for(User user : userList){
  System.out.println("Firstname:"+user.getFirstname());
 }
}

/******************************************************************/
/* Process custom result of Stored Procedure                      */
/* Un-comment the code, This will be the same for MSSQL and MYSQL */
/******************************************************************/
Query callStoredProcedure_MYSQL = session.createSQLQuery("CALL SP_MYSQL_HIBERNATE (:param1, :param2, :param3)");
callStoredProcedure_MYSQL.setInteger("param1", 10);
callStoredProcedure_MYSQL.setInteger("param2", 10);
callStoredProcedure_MYSQL.setString("param3", "javaQuery");

/* callStoredProcedure_MYSQL.list() will execute stored procedure and return the value */
List customResult = callStoredProcedure_MYSQL.list();
if (customResult != null && !customResult.isEmpty()) {
 Object[] obj = customResult.get(0);
 System.out.println(obj[0]);
 System.out.println(obj[1]);            
}