Java:PostgreSQL:Spring 引导:无法执行具有间隔和限制的本机查询
Java: PostgreSQL: Spring Boot: could not execute native query with interval and limit
我正在处理从 MySql 到 PostgreSQL 的迁移。因此,我正在存储库中实现 Spring Data 查询。我遇到了以下 native 查询的问题:
@Query(value = "UPDATE some_table SET some_field= :someValue, changed_at=now() WHERE (changed_at < now() - INTERVAL :timeout MINUTE) LIMIT :limit", nativeQuery = true)
int updateRecords(@Param("someValue") String someValue, @Param("timeout") int timeoutMinutes, @Param("limit") int limit);
它在 MySql 上运行良好,但在 PostgeSQL 上出现以下错误:
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement
...
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
...
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near ""
您不能以这种方式传递间隔的长度。
使用make_interval()
更容易:
changed_at < now() - make_interval(mins => :timeout)
或者,如果 JPA 也阻塞在 =>
changed_at < now() - (interval '1 minute' * :timeout)
我正在处理从 MySql 到 PostgreSQL 的迁移。因此,我正在存储库中实现 Spring Data 查询。我遇到了以下 native 查询的问题:
@Query(value = "UPDATE some_table SET some_field= :someValue, changed_at=now() WHERE (changed_at < now() - INTERVAL :timeout MINUTE) LIMIT :limit", nativeQuery = true)
int updateRecords(@Param("someValue") String someValue, @Param("timeout") int timeoutMinutes, @Param("limit") int limit);
它在 MySql 上运行良好,但在 PostgeSQL 上出现以下错误:
org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement
...
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
...
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near ""
您不能以这种方式传递间隔的长度。
使用make_interval()
更容易:
changed_at < now() - make_interval(mins => :timeout)
或者,如果 JPA 也阻塞在 =>
changed_at < now() - (interval '1 minute' * :timeout)