I'm trying to reproduce this sql query into a hibernate criteria query how can I achieve tha.
Code:
SELECT *
FROM PERSON
WHERE
BIRTH_DATE IS NOT NULL
AND (YEAR(GETDATE()) - YEAR(BIRTH_DATE)) in(6,18,26)
ORDER BY
MONTH(BIRTH_DATE) ASC,
DAY(BIRTH_DATE) ASC
Globally what I want is to get everybody in age of 6,18 and 26 years old ordered only by the month and the day not the year to have a list of future anniversaries.
In fact my problem in this situation is that I don't know how I can use the sql functions in criteria I know how criteria works but this query is quite tricky I don't have any idea how to reproduce it in criteria especially that I also have to implement this clause AND (YEAR(GETDATE()) - YEAR(BIRTH_DATE)) in(6,18,26) =/
Code:
Criteria criteriaBirthDates = his.getSession().createCriteria(Person.class);
criteriaBirthDates.add(Restrictions.isNotNull("birthdate"));
criteriaBirthDates.addOrder(Order.asc("month(birthdate)"));
criteriaBirthDates.addOrder(Order.asc("day(birthdate)"));
return criteriaBirthDates.list();
Many thanks!