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

前端 未结 2 1002
灰色年华
灰色年华 2020-12-01 11:11

I am developing an application in struts 2 and hibernate 3.

I have 3 tables

  1. Inspection
  2. InspectionMission
  3. Timeline

相关标签:
2条回答
  • 2020-12-01 11:49

    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.

    0 讨论(0)
  • 2020-12-01 11:54

    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".

    0 讨论(0)
提交回复
热议问题