Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA could not locate named parameter

I keep getting the following error: "could not locate named parameter [articleCommentId]" but it doesn't make sense to me because to me the named parameter is very much in place.

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);

}

Here is an extract of the stack trace with the relevant error:

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)
like image 973
Bill Rosmus Avatar asked Nov 15 '12 04:11

Bill Rosmus


3 Answers

I bet it is due to the extra ; in your query string.

SQL/HQL does not need to be terminated by semicolon

like image 103
Adrian Shum Avatar answered Nov 12 '22 01:11

Adrian Shum


The named parameters is not defined for native queries in JPA Specification.

Replace

where c.article_comment_id = :articleCommentId;

with

where c.article_comment_id = ?1;
....
query.setParameter(1, articleCommentId)
like image 45
Zaw Than oo Avatar answered Nov 12 '22 01:11

Zaw Than oo


Mine was an extra ' in the sql query. Oh my gosh, kept looking until my eyes nearly pooooopped out `-)

So, ensure that you don't have anything "extra" in your query, make sure that your (, ", ' etc...have matching pairs, because the error message in that case is not relevant and has nothing to do with your named parameter! JPA is right as it could not locate it, but that's because something else in your query is messing up...

like image 1
jumping_monkey Avatar answered Nov 11 '22 23:11

jumping_monkey