So I'm kind of new to hibernate, hence just bear with me here. I have a mysql query like so:
SELECT SUM(PLAN_ID), USER_ID
FROM TABLE_A
WHERE ACTIVATION_DATE
BETWEEN DATE_ADD(LAST_DAY(DATE_SUB(NOW(), INTERVAL 2 MONTH)), INTERVAL 1 DAY)
AND LAST_DAY(DATE_SUB(NOW(), INTERVAL 1 MONTH))
AND USER_ID in ('123','124') GROUP BY '123';
I'm trying to run the same using the hibernate 5 JPQL query. This is what I've tried:
Map<Integer, List<Object>> result = null;
Query query = null;
Session session = null;
session = SessionFactoryUtil.getSessionFactory().openSession();
result = new HashMap<Integer, List<Object>>();
query = session.createQuery(aggregationQuery);
int rowNo = 0;
for (Iterator it = query.iterate(); it.hasNext();) {
rowNo ++;
result.put(rowNo, (List<Object>) it.next());
}
Yet it throws Unexpected token errors
. Would someone mind explaining what im doing wrong here?
Thanks a bunch.
You cannot use them as they are because JPQL does not know them.
You have two options:
1) use SQL
session.createNativeQuery(aggregationQuery);
2) use the JPQL function
function('DATE_ADD', function('LAST_DAY', function('DATE_SUB', current_date, function('INTERVAL', 2, 'MONTH')), function('INTERVAL' 1, DAY)
Option 2 is not working code it's just and example to show you where this could lead. You probably use better option 1 with the native query.