I have a question about using Restrictions in Hibernate.
I have to create criteria, set some restrictions, and then choose one record with max value of Date field:
Criteria query = session.createCriteria(Storage.class)
query.add(Restrictions.eq(USER_ID, item.getUserId()));
query.add(Restrictions.eq(ITEM_ID, item.getItemId()));
query.add(Restrictions.lt(PURCHASE_DATE, item.getDate()));
query.setProjection(Projections.max(PURCHASE_DATE));
return query.uniqueResult();
I understand that Projections will not return me a record with max Date. What approach should I choose?
Maybe I can add order to my query (ascending order on Date field) and then choose the first result?
Or could you please suggest more elegant solution?
Thank you!