2016-10-10 59 views
1

我遇到了執行hibernate(5.0.8)中的存儲過程的問題。 下面是相同的代碼...無法使用Hibernate 5.0.8執行存儲過程

MyDao.java

@SuppressWarnings("unchecked") 
public List getList() { 

    ProcedureCall procedureCall = getSessionFactory() 
      .getCurrentSession() 
      .createStoredProcedureCall(
        "mysp_get_stored_proc", 
        MyClass.class); 
    procedureCall.registerParameter("p_code_category",String.class, ParameterMode.IN).bindValue("WORLD"); 
    procedureCall.registerParameter("p_cdt_language", String.class, ParameterMode.IN).bindValue("FRENCH"); 
    procedureCall.registerParameter("p_lst", Class.class, ParameterMode.REF_CURSOR); 
    procedureCall.registerParameter("p_err_code_out", Integer.class, ParameterMode.OUT); 
    procedureCall.registerParameter("p_err_msg_out", String.class, ParameterMode.OUT); 


    ProcedureOutputs procedureOutputs = procedureCall.getOutputs(); 
    ResultSetOutput resultSetOutput = (ResultSetOutput) procedureOutputs.getCurrent(); 
    List list = resultSetOutput.getResultList(); 

    return list; 
} 

我的存儲過程簽名:

PROCEDURE mysp_get_stored_proc (
    p_code_category IN  code_table.code_cat_cd%TYPE, 
    p_cdt_language IN object_intr.ct_language_cd%TYPE, 
    p_lst     OUT  ref_cur, 
    p_err_code_out   OUT  NUMBER, 
    p_err_msg_out   OUT  VARCHAR2 

錯誤消息:

java.sql.SQLException: The number of parameter names does not match the number of registered praremeters 
at oracle.jdbc.driver.OracleSql.setNamedParameters(OracleSql.java:198) 
at oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:4712) 
at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:1376) 
at com.ibm.ws.rsadapter.jdbc.WSJdbcPreparedStatement.pmiExecute(WSJdbcPreparedStatement.java:958) 
at com.ibm.ws.rsadapter.jdbc.WSJdbcPreparedStatement.execute(WSJdbcPre 

有人可以幫助在哪裏和什麼是錯誤的....?

回答

0

我認爲你沒有使用ref_cursor的正確方法。下面是你如何使用ref_cursor的例子。

CREATE OR REPLACE FUNCTION get_reviews(bookid bigint) 
RETURNS refcursor AS 
$BODY$ 
DECLARE 
    reviews refcursor;   -- Declare cursor variables       
BEGIN 
    OPEN reviews FOR SELECT id, comment, rating, version, book_id FROM review  WHERE book_id = bookId; 
    RETURN reviews; 
END; 
$BODY$ 
LANGUAGE plpgsql 

命名程序查詢:

@NamedStoredProcedureQuery(
name = "getReviews", 
procedureName = "get_reviews", 
resultClasses = Review.class, 
parameters = { 
    @StoredProcedureParameter(mode = ParameterMode.REF_CURSOR, type = void.class), 
    @StoredProcedureParameter(mode = ParameterMode.IN, type = Long.class) 
} 
) 

這就是你如何使用查詢:

List<Book> books = (List<Book>)   this.em.createNamedStoredProcedureQuery("getBooks").getResultList(); 
    for (Book b : books) { 
    StoredProcedureQuery q = this.em.createNamedStoredProcedureQuery("getReviews"); 
q.setParameter(2, b.getId()); 
List<Review> reviews = q.getResultList(); 
for (Review r : reviews) { 
    // do something 
} 
} 
+0

感謝Pushpendra ......最終,該存儲過程運行良好的SQL開發人員。 ..此外它已被使用或執行通過標準的JDBC CallableStatements ....我只是想運行它使用Hibernate ,,,,你可能會認爲SP運行良好.... :) –

+0

任何建議或內部Java或Hibernate End可能會出錯。 ....? –

+0

Folks ....我設法修改了一些東西...現在我得到這個錯誤... –