I need the possibility to retrieve the orders for the past 6 months for my backend application.
@Query("Select ord From orders ord WHERE ord.created_date <= (current_date - INTERVAL '6 months')") List<Order> findOrders();
unexpected token: INTERVAL near line 1, column 70 [Select ord From orders ord WHERE ord.created_date <= (current_date INTERVAL '6 months')
After some research , I’ve found out that JPA does not support the INTERVAL definition. Is there a workaround for archiving this particularly function ?
Advertisement
Answer
In that case use the JPA provided functionality , where native sql code is not required.
List<Order> findALLByCreatedDateBefore(Date fromDate);
and on java side invoke it
public void findOrders() { date = setMonthToJavaUtilDate(new Date(), -6); yourRepository.findOrders(fromDate) //your logic... } private Date setMonthToJavaUtilDate(Date date, int month) { Calendar now = Calendar.getInstance(); now.setTime(date); now.add(Calendar.MONTH, month); return now.getTime(); }