【问题标题】:JPA could not locate named parameterJPA 找不到命名参数
【发布时间】:2012-11-03 16:34:26
【问题描述】:

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

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)

【问题讨论】:

    标签: java hibernate jpa


    【解决方案1】:

    在我的例子中,我没有在命名参数后添加额外的空格。

    示例:

      + "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"                                        
    

    【讨论】:

      【解决方案2】:

      我的是 sql 查询中的一个额外的'。哦,我的天哪,一直看,直到我的眼睛都快掉出来了`-)

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          你也可以这样使用

          其中 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)

          【讨论】:

            【解决方案5】:

            没有为JPA Specification 中的本机查询定义命名参数。

            替换

            where c.article_comment_id = :articleCommentId;
            

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

            【讨论】:

            • 是的,这在规范中。当我看到您的答案并找到另一个引用该规范部分的答案时,我对此进行了研究。所以给你指点。但是对于 Postgres + hibernate,它可以容忍命名参数。并且命名参数使代码更具可读性。所以我会坚持下去。不过很好的指针。
            【解决方案6】:

            我敢打赌,这是因为您的查询字符串中有额外的 ;

            SQL/HQL 不需要以分号结束

            【讨论】:

            • 就是这样。奇怪的是,在另一个本机查询中,这没有问题。但是对于那个,参数位于查询的中间(递归查询的根选择)。我想我只是将它包含在其中并且它起作用了。作为人类,我只记得最后一件有效的事情,因为在我需要的其他几个本地查询中,我没有包含分号。当然,他们工作。我发现@CycDemo 的答案在技术上是正确的,另一个答案显示了说明这一点的规范。但是对于 Postgres + hibernate 命名参数似乎可以工作并且使代码更易于理解。
            猜你喜欢
            • 2015-03-29
            • 2020-06-04
            • 2012-09-08
            • 2017-11-21
            • 2016-10-21
            • 2013-09-11
            • 2017-12-30
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多