繁体   English   中英

错误:带有参数的“$1”postgresql 查询处或附近的语法错误

[英]ERROR: syntax error at or near "$1" postgresql query with params

我有一个带有参数的 postgres 查询,在我的 Java 代码中,我使用 stringBuilder 来构建查询,然后使用 createNativeQuery 来创建查询并填充参数。 但是,如果我在 gui 中运行查询,它会给我预期的结果,但在通过代码执行时会出错。 我是 postgres 的新手,看到了一些与我需要在查询中转换参数几乎相同的问题,但我的查询中有 IN 参数,即列表,我尝试了一些解决方案,但没有没用。

我收到错误:-> "error": "Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: org.postgresql. util.PSQLException:错误:“$1”处或附近的语法错误位置:249

        StringBuilder sb = new StringBuilder();
        sb.append(" SELECT comments.comment, mapping.user_id, 
        mapping.comment_id, mapping.comment_state, 
        mapping.created_at  FROM comments ").append(" comment 
        JOIN ").
        append(" user_comment_mapping  mapping ON 
        mapping.comment_id = comment.id WHERE 
        mapping.user_id IN ? AND mapping.comment_state = ? 
        ");

        final Query query = em.createNativeQuery(sb.toString());
        query.setParameter("1", userIds);
        query.setParameter("2", 
        2);

        List<Object[]> list = query.getResultList();

谢谢!

看来您在参数持有者和名称之间混用了。

如果你想使用 holder :

mapping.user_id IN ? AND mapping.comment_state = ? 

然后

query.setParameter(1, userIds);
query.setParameter(2, 2);

如果要使用名称:

mapping.user_id IN :ids AND mapping.comment_state = :stat


query.setParameter("ids", userIds);
query.setParameter("stat", 2);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM