是否可以从自定义JPA查询返回 map 而不是列表?

我知道实体本身是否可能。在我的情况下,我有一个自定义查询,该查询在不同的表中返回日期范围内的一些统计信息。

理想情况下,我希望返回的 map 将日期作为键,并将统计信息作为值。

最佳答案

您只需要自己创建和填充 map 即可:

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);
}

10-07 19:34
查看更多