Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
like image 537
Ramon De Les Olives Avatar asked Mar 07 '23 01:03

Ramon De Les Olives


1 Answers

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.
like image 91
asm0dey Avatar answered Mar 10 '23 02:03

asm0dey