如何获取休眠查询结果作为列表或哈希图的关联数组
我正在使用 struts 2 和 hibernate 3 开发应用程序.
I am developing an application in struts 2 and hibernate 3.
我有 3 张桌子
- 检查
- 检查任务
- 时间表
Inspection
与 InspectionMission
相关联,InspectionMission
与 Timeline
相关联.
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".