Spring Data JPA - 查询日期减去 2 天不起作用 [英] Spring Data JPA - query with the date minus 2 days not working

查看:35
本文介绍了Spring Data JPA - 查询日期减去 2 天不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个查询更新了比 2 天前更高的价格,但不起作用

I have this query that updates some prices higher that 2 days ago, but does not work

  @Transactional
    @Modifying
    @Query("update HotelDailyPrice hdp  set hdp.price = (select avg (hp.price) "
            + "from HotelPrice hp where hp.id = ?1 and hp.updateDate > CURRENT_DATE - 2), hdp.day = ?2 ")
    void updateDailyAveragePrice (Hotel hotel, String dayDate);

推荐答案

实际上,JPA 不支持时间段操作,因为并非所有数据库都支持它.所以你有以下选择:

Actually, JPA doesn't support time periods operations because not all databases support it. So you have following options:

  1. 以编程方式计算日期(即使用日历 API 或 Java 8 日期时间 API)
  2. 使用原生查询
  3. 如果您的 JPA 提供程序允许,请使用具体实现.但是 AFAIK 流行的 JPA 提供程序没有给您这样的选择.

这篇关于Spring Data JPA - 查询日期减去 2 天不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆