5

Is it possible to return a map instead of a List from a custom JPA Query?

I know if is possible from the Entities themselves. In my case I have a custom query which returns some stats across different tables for a range of dates.

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

1 Answer 1

5

You'll just have to create and populate the map by 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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's what I gathered. I was hoping there might be a clever way to avoid the overhead. Tx.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.