java JPA 找不到命名参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13391384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:32:38  来源:igfitidea点击:

JPA could not locate named parameter

javahibernatejpa

提问by Bill Rosmus

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.

我不断收到以下错误:“找不到命名参数 [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);

}

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)

回答by Adrian Shum

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

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

SQL/HQL does not need to be terminated by semicolon

SQL/HQL 不需要以分号结束

回答by Zaw Than oo

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

中未为本地查询定义命名参数JPA Specification

Replace

代替

where c.article_comment_id = :articleCommentId;

with

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

回答by neha yadav

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)

其中 c.article_comment_id = ?, 和 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)

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

回答by Aditya Patil

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

如果您在查询结束时使用命名参数,请删除 ; 从您的查询