我正在用 struts 2 和 hibernate 3 开发一个应用程序。
我有3张 table
- 检查
- 检查任务
- 时间表
Inspection
与 InspectionMission
关联,InspectionMission
与 Timeline
关联。
现在我有以下问题。我在 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中显示如下:
在JSP中我写了如下代码
<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>
这里有人能帮帮我吗?
最佳答案
您必须使用“新 map ”语法 (Hibernate Reference paragraph 14.6)
select new map(count(i.inspectionId) as tot_inspections, t.year as year, t.quarter as quarter) from ...
查询的其余部分是相同的。这将返回一个映射列表,其中键是“列”的别名。
关于java - 如何将 hibernate 查询结果作为列表或 HashMap 的关联数组获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/925363/