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

2

17

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

I have 3 tables

  1. Inspection
  2. InspectionMission
  3. Timeline

Inspection is associated with InspectionMission and InspectionMission is associated with Timeline.

Now I have following problem. I have written following query in HQL

public List getQuartewiseInspectionList(){

   Session session = HibernateUtil.getSessionFactory().getCurrentSession();

   Query q = session.createQuery(
                "select count(i.inspectionId) as tot_inspections,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");

   return q.list();

}

I want to fetch result as following

result[0][tot_inspections] = "6"
result[0][year] = "2009";
result[0][quarter] = "Q2";

result[1][tot_inspections] = "3"
result[1][year] = "2009";
result[1][quarter] = "Q3";

and so on so that I can display it in jsp struts as follows:

In JSP I have written following code

<table border="1">

   <s:iterator value="result" status="status">
       <tr class="<s:if test="#status.even">even</s:if><s:else>odd</s:else>">
             <td class="nowrap"><s:property value="tot_inspections" /></td>
             <td class="nowrap"><s:property value="year" /></td>
             <td class="nowrap"><s:property value="quarter" /></td>
       </tr>         
    </s:iterator>
</table>

Can anyone here help me?

Allayne answered 29/5, 2009 at 10:34 Comment(0)
E
35

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

Eudemonia answered 29/5, 2009 at 10:43 Comment(4)
Hello Salvatore Insalaco, Thanks for your answer. I was searching for last 2 days for this solution and your answer provided it to me. Thanks for the perfect answer.Allayne
This was exactly what I needed, but couldn't find in the Hibernate docs.Helsell
Someone please post a link this hibernate topic.Trickish
docs.jboss.org/hibernate/orm/3.3/reference/en/html/…Ealdorman
L
1

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.

Longe answered 29/5, 2009 at 12:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.