Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...