Can a Hibernate user request return a map instead of a list?

Is it possible to return a map instead of a list from a custom JPA request?

I know if this is possible from the Beings themselves. In my case, I have a custom query that returns some statistics in different tables for a number of dates.

Ideally, I would like the returned map to have a date as a key and a stat value as a value.

+5
source share
1 answer

You just need to create and fill out the map yourself:

List<Object[]> rows = query.list();
Map<Date, Integer> statsPerDate = new HashMap<Date, Integer>(rows.size());
for (Object[] row : rows) {
    Date date = (Date) row[0];
    Integer stat = (Integer) row[1];
    statsPerDate.put(date, stat);
}
+5
source

All Articles