8

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

6 回答 6

23

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

SQL/HQL 不需要以分号结束

于 2012-11-15T04:07:39.540 回答
7

命名参数没有为JPA Specification.

代替

where c.article_comment_id = :articleCommentId;

where c.article_comment_id = ?1;
....
query.setParameter(1, articleCommentId)
于 2012-11-15T06:03:13.603 回答
1

'我的是sql 查询中的一个额外的。哦,我的天哪,一直看,直到我的眼睛差点漏出来`-)

因此,请确保您的查询中没有任何“额外”,确保您的 (, ", ' 等...具有匹配对,因为这种情况下的错误消息不相关并且与你的命名参数!JPA 是正确的,因为它找不到它,但那是因为你的查询中的其他东西搞砸了......

于 2020-09-04T07:27:47.083 回答
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)

于 2020-02-15T11:07:48.837 回答
0

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

于 2020-04-06T06:55:06.543 回答
0

就我而言,我没有在命名参数之后添加额外的空格。

例子:

  + "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"                                        
于 2021-10-25T14:37:29.847 回答