Bill Rosmus
Bill Rosmus

Reputation: 3011

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)

Upvotes: 12

Views: 32939

Answers (6)

Raghu
Raghu

Reputation: 1393

In my case, I didn't add the extra space after the named parameter.

example:

  + "WHERE\n" + "    s.something = 'SOME'\n" + "START WITH\n"
                                        + "    s.country_type = :countryName" + "CONNECT BY\n"
                                        
changed to (notice the space after named parameter :countryName 

  + "WHERE\n" + "    s.something = 'SOME'\n" + "START WITH\n"
                                        + "    s.country_type = :countryName " + "CONNECT BY\n"                                        

Upvotes: 0

jumping_monkey
jumping_monkey

Reputation: 7819

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...

Upvotes: 1

Aditya Patil
Aditya Patil

Reputation: 1486

If you are using named parameter at end of your query the remove the ; from your query

Upvotes: 1

neha
neha

Reputation: 60

You can also use it like this

where c.article_comment_id = ?, and c.any_other_field = ?; .... query.setParameter(1, articleCommentId) query.setParameter(2, anyOtherValue)

it will take it by sequence.

And you can also give numbers like

where c.article_comment_id = ?1, and c.any_other_field = ?2; .... query.setParameter(1, articleCommentId) query.setParameter(2, anyOtherValue)

Upvotes: 0

Zaw Than oo
Zaw Than oo

Reputation: 9935

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)

Upvotes: 8

Adrian Shum
Adrian Shum

Reputation: 40036

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

SQL/HQL does not need to be terminated by semicolon

Upvotes: 35

Related Questions