2012-11-15 56 views
3

我不斷收到以下錯誤:「無法找到命名參數[articleCommentId]」但它對我來說沒有意義,因爲對我來說命名參數非常到位。JPA無法找到命名參數

public ArticleCommentForDisplay getCommentByArticleCommentId(BigInteger articleCommentId) { 

    String queryString = "select c.article_comment_id, " 
      + "  c.article_id, " 
      + "  c.parent_comment_id, " 
      + "  p.nickname, " 
      + "  c.title, " 
      + "  c.comment, " 
      + "  c.person_id, " 
      + "  c.confirmed_user, " 
      + "  c.comment_depth, " 
      + "  c.moderation_rank, " 
      + "  c.moderation_reason, " 
      + "  c.hide, " 
      + "  c.hide_reason, " 
      + "  c.session_id, " 
      + "  c.confirmation_uuid, " 
      + "  c.created_timestamp, " 
      + "  c.created_by_id, " 
      + "  c.updated_timestamp, " 
      + "  c.updated_by_id, " 
      + "  c.update_action, " 
      + "  null as comment_path " 
      + "from article_comment c " 
      + " join person p " 
      + "  on p.person_id = c.person_id " 
      + "where c.article_comment_id = :articleCommentId; "; 

    Query query = em.createNativeQuery(queryString, "ArticleCommentMap"); 
    query.setParameter("articleCommentId", articleCommentId); 

    List <ArticleCommentForDisplay> articleComments = new ArrayList<>(); 
    articleComments = query.getResultList(); 
    ArticleCommentForDisplay theComment = articleComments.get(0); 

    return (theComment); 

} 

下面是相關的錯誤堆棧跟蹤的摘錄:

Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: could not locate named parameter [articleCommentId] 
    at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:379) 
    at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:72) 
    at com.extremelatitudesoftware.content.ArticleCommentFacade.getCommentByArticleCommentId(ArticleCommentFacade.java:293) 

回答

7

我敢打賭,這是由於您的查詢字符串的額外;

SQL/HQL不需要用分號

+0

就是這樣。奇怪的是,在另一個本地查詢中,這沒有問題。但是對於那個,參數在查詢中間(遞歸查詢中的根選擇)。我想我只是把它包括在裏面,它工作。作爲人類,我只記得最後一件工作,因爲在我需要的其他幾個本地查詢中,我沒有包含分號。當然,他們工作。 @我發現CycDemo的答案在技術上是正確的,另一個答案顯示規範說明了這一點。但對於Postgres + hibernate命名參數似乎可以工作,並且可以製作更易於理解的代碼。 – BillR

7

被終止的命名參數不JPA Specification爲原生查詢定義。

更換

where c.article_comment_id = :articleCommentId; 

where c.article_comment_id = ?1; 
.... 
query.setParameter(1, articleCommentId) 
+0

是的,這是在規範。當我看到你的答案時,我研究了這一點,並找到了引用該規範部分的另一個答案。所以指向你。但對於Postgres +休眠它容忍命名參數。命名參數使代碼更具可讀性。所以我會堅持下去。好的指針雖然。 – BillR

相關問題