2012-11-15 61 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

相关问题