I can't figure out how to do date operations using the JPA CriteriaBuilder API.
Basically, I have two date fields in the database, and I want to add a restriction where one date is within 7 days of the other. Here is the code snippet:
Code:
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Object> criteria = builder.createQuery();
Root<MyEntity> root = criteria.from(MyEntity.class);
Path<Date> date1 = root.get("date1");
Path<Date> date2 = root.get("date2");
// This line is a compile error because CriteriaBuilder.diff() only applies to Numbers:
Expression<Date> date2MinusOneWeek = builder.diff(date2, 7);
criteria.where(builder.greaterThan(date1, date2MinusOneWeek));
I don't see a way to do that subtraction, though, since the sum and difference functions in CriteriaBuilder only accept subclasses of Number.
I'm using postgres, so I really just need it to generate something like "WHERE date1 > date2 - 7" in the end.
Any ideas?