Java PSQLException尝试在查询Spring引导JPA中插入字符串

Java PSQLException尝试在查询Spring引导JPA中插入字符串,java,postgresql,spring-boot,hibernate,Java,Postgresql,Spring Boot,Hibernate,我想找到所有特定列(即时间戳)大于特定时间戳的记录。。。所有这些都是使用PostgreSQL实现的 我拥有以下存储库: 公共接口IssuePublicationRepository扩展了IssueRepository{ @查询(value=“SELECT*FROM issue WHERE created_at>=timestamp?1”,nativeQuery=true) 列出findByMaxCreatedAt(字符串createdAt); } 我是这样使用它的: issueReposito

我想找到所有特定列(即时间戳)大于特定时间戳的记录。。。所有这些都是使用PostgreSQL实现的

我拥有以下存储库:

公共接口IssuePublicationRepository扩展了IssueRepository{
@查询(value=“SELECT*FROM issue WHERE created_at>=timestamp?1”,nativeQuery=true)
列出findByMaxCreatedAt(字符串createdAt);
}
我是这样使用它的:

issueRepository.findByMaxCreatedAt(
新的简化格式(“yyyy-MM-dd HH:MM:ss”,Locale.ITALY)
.format(新日期(Instant.now().toEpochMilli()-10L*30*24*60*60*1000)
)
); // 只是一个随机的约会
然而,我得到:

2021-05-26 13:23:39.431 DEBUG 96462 --- [nio-8080-exec-1] org.hibernate.SQL                        : SELECT * FROM issue WHERE created_at >= timestamp ?
Hibernate: SELECT * FROM issue WHERE created_at >= timestamp ?
2021-05-26 13:23:39.432 TRACE 96462 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [2020-07-30 13:23:39]
2021-05-26 13:23:39.438  WARN 96462 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 42601
2021-05-26 13:23:39.438 ERROR 96462 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: syntax error at or near "$1"
  Posizione: 51
2021-05-26 13:23:39.457 ERROR 96462 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause

org.postgresql.util.PSQLException: ERROR: syntax error at or near "$1"
但是如果我跑

SELECT * FROM issue WHERE created_at >= timestamp '2020-07-30 13:21:41'

在我的DBMS上,它工作正常

如果确实必须使用字符串,则必须强制转换参数:

@Query(value = "SELECT * FROM issue WHERE created_at >= cast(?1 as timestamp)", nativeQuery = true)

尝试使用
@Query(value=“SELECT i FROM issue i WHERE i.createdAt>=?1”)列表findByMaxCreatedAt(LocaleDateTime createdAt)我认为您应该像
SELECT*FROM issue WHERE created_at>=timestamp'?1'
那样引用它。或者最好不要使用
String
作为参数类型,并使用
Timestamp
LocalDateTime
。因此,查询将是
SELECT*FROM issue WHERE created\u at>=?1
@YCF\u L postgresql将在一分钟内复制结果cell@Alex我会尽快尝试的。我会尽快尝试