I want to write a method that returns a list of last added objects grouped by field 'serviceId'.
The following HQL works, but I want to write this using Criteria API:
FROM Notification WHERE date IN
(SELECT MAX(date) FROM Notification GROUP BY serviceId)
ORDER BY date ASC
Something like this:
Criteria crit = session.createCriteria(Notification.class);
crit.add(Restrictions.in("date", <MAX dates>));
criteria.addOrder(Order.desc("date"));
Thanks in advance.
EDIT:
Now I need a similar query that works using eclipselink API =/
Basically, I need the last N rows (max date), which status is one of the five described bellow, grouped by serviceId column.
Due to my inexperience, it was the best I could:
ExpressionBuilder builder = new ExpressionBuilder();
Expression exStatus1 = builder.get("status").equal(MessageType.START.toString());
Expression exStatus2 = builder.get("status").equal(MessageType.RUNNING.toString());
Expression exStatus3 = builder.get("status").equal(MessageType.PAUSED.toString());
Expression exStatus4 = builder.get("status").equal(MessageType.END_ERROR.toString());
Expression exStatus5 = builder.get("status").equal(MessageType.END_SUCCESS.toString());
ReadAllQuery query = new ReadAllQuery();
query.setReferenceClass(Notification.class);
query.setSelectionCriteria(((exStatus1).or(exStatus2).or(exStatus3).or(exStatus4).or(exStatus5)));
query.setMaxRows(listSize);
query.addDescendingOrdering("date");
The clause to avoid duplicates serviceIds in result rows is missing...
You're going to want to use the Criteria projections API with a detached subquery:
This mirrors the technique you are using in your HQL query.
EDIT:
I updated the query to match serviceId between your main notification class and your sub query, essentially the same as this HQL Query:
This prevents the case where you would have a non-maximum date matching between two different serviceIds like so:
Old query return:
New query return:
Let me know if this works for you.