I need to do a query where i take things older then 5 days.(a custom date)
i have looked at HQL but since it is in persistence i dont have access to setTimestamp as per here
String hqlQuery;
Calendar minDate = Calendar.getInstance();
minDate.add(Calendar.DATE, -days);
hqlQuery = "select n from notifications where n.app_id=:app and ((n.sent_date<=:minDate) OR (n.sent_date is null)) and n.handled='N'";
is how i tried…
Any sweet helpers, thanks in advance 🙂
Edit:
So i changed my method it now looks like this:
public static List<Notification> findOlderThen(EntityManager em, Long app, int days) {
String hqlQuery;
Calendar minDate = Calendar.getInstance();
minDate.add(Calendar.DATE, -days);
hqlQuery = "select n from notifications where n.app_id=:app and ((n.sent_date<=:minDate) OR (n.sent_date is null)) and n.handled='N'";
System.out.println(hqlQuery);
return em.createQuery(hqlQuery).setParameter("app", app).setParameter("minDate",minDate.getTime()).getResultList();
}
But it gives this error:
Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing the query [select n from notifications where n.app_id=:app and ((n.sent_date<=:minDate) OR (n.sent_date is null)) and n.handled='N'], line 1, column 28: syntax error at [where].
Internal Exception: UnwantedTokenException(found=where, expected 80)
Would be how you set a date parameter, assuming type of sent_date is Date in Notifications model class. Also, on this part of your query
select n from notifications, make sure notifications is the actual name of your class. Case matters! It should probably beselect n from Notifications n.Update:
You need to declare the alias for the class.