gen*_* b. 5 postgresql spring-data spring-data-jpa
我需要将动态参数传递给 Spring Data Postgres 本机查询。参数位于区间表达式中。
在运行代码时,我从 pgAdmin 验证以下内容仅返回 1 个结果(正确):
select * from recalls_t where created_date <= (now() - interval '18 hour')
问题:
1)在Spring Data代码中,以下?1表示法错误地返回2个结果:
@Query(value="select * from recalls_t where created_date <= (now() - interval '?1 hour')",
nativeQuery=true)
public List<RecallsT> findActiveRecallsInLastHrs(@Param("hours") int hours);
Run Code Online (Sandbox Code Playgroud)
2)然后我尝试了这个线程中的解决方案:Postgres Interval not work with native spring data JPA query
他们说要使用单个单位间隔的倍数。但这也会错误地返回 2 个结果:
@Query(value="select * from recalls_t where created_date <= (now() -
(interval '1 hour') * :hours)",
nativeQuery=true)
public List<RecallsT> findActiveRecallsInLastHrs(@Param("hours") int hours);
Run Code Online (Sandbox Code Playgroud)
我发现解决此问题的唯一方法是在 Spring Data 查询中对数字“18”进行硬编码。所以现在没有动态参数,我正确地得到了 1 个结果。所有动态参数实现都不起作用。
Edw*_*ung -1
尝试传递一个字符串?
@Query(value="select * from recalls_t where created_date <= (now() - interval ?1)",
nativeQuery=true)
public List<RecallsT> findActiveRecallsInLastHrs(String interval);
// ...
findActiveRecallsInLastHrs("1 hour");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1761 次 |
| 最近记录: |