首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >hibernate查询中命名参数中的单引号出现错误

hibernate查询中命名参数中的单引号出现错误
EN

Stack Overflow用户
提问于 2021-04-15 13:54:36
回答 1查看 227关注 0票数 0

我需要在timeBucket周围有一个单独的引用。我有以下原生sql,它工作得很好。

代码语言:javascript
复制
entityManager.createNativeQuery("SELECT time_bucket('" + timeBucket + "', time) as timestamp, avg(cast(value as double precision)) as value" +
            " FROM agent_data " +
            " WHERE agent_id = :agentId and topic_id = :topicId" +
            " GROUP BY timestamp " +
            " ORDER BY timestamp DESC " +
            " LIMIT :noOfRecords", "GetAgentDataMapping")
            .setParameter("noOfRecords", noOfRecords)
            .setParameter("agentId", agentId)
            .setParameter("topicId", topicId)
            .getResultList();

我连接timeBukcket参数是因为如果我使用setParameter方法绑定该变量,则会出现错误

代码语言:javascript
复制
"Could not locate ordinal parameter [1], expecting one of [2, 3, 4, 5, 6]; nested exception is java.lang.IllegalArgumentException: Could not locate ordinal parameter [1], expecting one of [2, 3, 4, 5, 6]"

我尝试使用sql字符串连接,如下所示。但它失败了,并出现了相同的错误:

代码语言:javascript
复制
em.createNativeQuery("SELECT time_bucket('|| :timeBucket ||', time) as timestamp, avg(cast(value as double precision)) as value" +
            " FROM agent_data " +
            " WHERE agent_id = :agentId and topic_id = :topicId" +
            " GROUP BY timestamp " +
            " ORDER BY timestamp DESC " +
            " LIMIT :noOfRecords", "GetAgentDataMapping")
            .setParameter("timeBucket", timeBucket)
            .setParameter("noOfRecords", noOfRecords)
            .setParameter("agentId", agentId)
            .setParameter("topicId", topicId)
            .getResultList();

我还使用了SELECT time_bucket('''|| :timeBucket ||''', time)来转义单引号。

但是同样的错误,没有用。Hibernate对于如此简单的事情没有快速的解决方案,这真的很令人沮丧。任何建议都会有帮助。

EN

回答 1

Stack Overflow用户

发布于 2021-04-16 17:17:31

我猜你是在用时间刻度吧?使用以下内容有什么问题?

代码语言:javascript
复制
em.createNativeQuery("SELECT time_bucket(:timeBucket, time) as timestamp, avg(cast(value as double precision)) as value" +
        " FROM agent_data " +
        " WHERE agent_id = :agentId and topic_id = :topicId" +
        " GROUP BY timestamp " +
        " ORDER BY timestamp DESC " +
        " LIMIT :noOfRecords", "GetAgentDataMapping")
        .setParameter("timeBucket", timeBucket)
        .setParameter("noOfRecords", noOfRecords)
        .setParameter("agentId", agentId)
        .setParameter("topicId", topicId)
        .getResultList();

AFAIU time_bucket的参数需要是字符串或间隔。也许您需要将参数强制转换为一个间隔cast(:timeBucket as interval)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67102961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档