JPA无法找到命名参数

时间:2012-11-15 04:03:14

标签: java hibernate jpa

我一直收到以下错误:“无法找到命名参数[articleCommentId] ”但它对我没有意义,因为对我来说命名参数非常到位。

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)

5 个答案:

答案 0 :(得分:8)

我敢打赌这是由于查询字符串中的额外;

SQL / HQL不需要以分号

终止

答案 1 :(得分:7)

JPA Specification中的本机查询未定义命名参数。

替换

where c.article_comment_id = :articleCommentId;

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

答案 2 :(得分:0)

您也可以像这样使用它

其中c.article_comment_id =?, 和c.any_other_field =?; .... query.setParameter(1,articleCommentId) query.setParameter(2,anyOtherValue)

它将按顺序处理。

您也可以给数字

其中c.article_comment_id =?1, 和c.any_other_field =?2; .... query.setParameter(1,articleCommentId) query.setParameter(2,anyOtherValue)

答案 3 :(得分:0)

如果在查询末尾使用命名参数,请删除;。从您的查询中

答案 4 :(得分:0)

在SQL查询中,我的'是额外的。哦,我的天哪,一直看着,直到我的眼睛几乎po出来`-)

因此,请确保您的查询中没有任何“ extra ”,并确保您的(,“,'等...具有匹配对,因为该错误消息情况不相关且与您的命名参数无关!JPA是正确的,因为它无法找到它,但这是因为查询中的其他内容正在混乱...

相关问题