How to fetch hibernate query result as associative array of list or hashmap

You have to use the "new map" syntax (Hibernate Reference paragraph 14.6)

select new map(count(i.inspectionId) as tot_inspections, t.year as year, t.quarter as quarter) from ...

The rest of the query is the same. This will return a list of maps, where the key is the alias of the "column".


Another solution would be to define a data object just for displaying those results and let Hibernate create instances of those on the fly. This class would just need a matching constructor.

Example class (getters and fields omitted)

public class InspectionCount() {
    // fields
    public InspectionCount(int count, int year, int quarter) {
        // initialize instance
    }
    // getters
}

The query would then look

select new InspectionCount(count(i.inspectionId), t.year, t.quarter)
        from Inspection as i
        inner join i.inspectionMission as im inner join im.timeline as t
        group by t.year,t.quarter

As a result you would get a List of InspectionCounts.