如何获取休眠查询结果作为列表或哈希图的关联数组 [英] How to fetch hibernate query result as associative array of list or hashmap

查看:29
本文介绍了如何获取休眠查询结果作为列表或哈希图的关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 struts 2 和 hibernate 3 开发应用程序.

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

我有 3 张桌子

  1. 检查
  2. 检查任务
  3. 时间表

InspectionInspectionMission 相关联,InspectionMissionTimeline 相关联.

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

现在我有以下问题.我在 HQL 中编写了以下查询

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

}

我想获取如下结果

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

等等,这样我就可以在jsp struts中显示如下:

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

在JSP中我写了以下代码

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?

推荐答案

你必须使用新地图"语法 (Hibernate 参考第 14.6 段)

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

这篇关于如何获取休眠查询结果作为列表或哈希图的关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆