Spring Data JPA - query with the date minus 2 days not working

南楼画角 提交于 2021-02-07 09:30:41

问题


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);

回答1:


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

  1. Calculate date programmatically (i.e. using calendar API or Java 8 Date Time API)
  2. Use native query
  3. Use concrete implementation if your JPA provider allows that. But AFAIK popular JPA providers don't give you such an option.


来源:https://stackoverflow.com/questions/48463143/spring-data-jpa-query-with-the-date-minus-2-days-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!