【问题标题】:Could not execute query in spring boot framework无法在 Spring Boot 框架中执行查询
【发布时间】:2020-07-20 07:37:55
【问题描述】:
SELECT * FROM work_hour where start_time between '2020-04-06 23:03' and '2020-04-09 23:03';

这在将 search_path 设置为 foo 时有效 但是我想使用相同的查询而不是我想使用参数的字符串

所以我在我的服务函数中使用了这个注解:

@Query(value = "SELECT s FROM foo.work_hour s WHERE TO_TIMESTAMP(s.start_time,  'YYYY-MM-DD HH24:mi')\\:\\:timestamp BETWEEN :start_time\\:\\:timestamp AND now()\\:\\:timestamp", nativeQuery = true)

但这似乎行不通。

There was an unexpected error (type=Internal Server Error, status=500). could not execute query; SQL [SELECT s FROM logines.work_hour s WHERE TO_TIMESTAMP(s.start_time, 'YYYY-MM-DD HH24:mi')::timestamp BETWEEN ?::timestamp AND now()::timestamp]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query

org.postgresql.util.PSQLException: The column name id was not found in this ResultSet.

如何实现?

【问题讨论】:

  • 可能不相关,但是:to_timestamp() 用于将不是时间戳转换为时间戳的内容。在 is 已经是时间戳的列上调用它是完全错误的。我认为你应该用简单的:start_time 替换:start_time\\:\\:timestamp,然后传递java.time.LocalDateTime 的实例作为参数(或至少java.sql.Timestamp)。
  • 您是否尝试过实际搜索错误消息的含义?

标签: postgresql spring-boot jpa


【解决方案1】:

由于这是一个原生查询,您需要使用正确的(原生)SQL 语法。

select s from work_hour s

产生具有单个列的结果,该列是具有多个字段的记录。但是你想要多列,所以你必须使用

select s.* 
from work_hour s

查看此online example中输出的差异

【讨论】:

    猜你喜欢
    • 2013-01-03
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多