如何获取 Hibernate 查询结果作为列表或哈希图的关联数组
我正在 struts 2 和 hibernate 3 中开发一个应用程序。
我有 3 个表
- Inspection
- InspectionMission
- Timeline
Inspection
与 InspectionMission
关联,InspectionMission
与时间线
。
现在我有以下问题。 我在 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>
这里有人可以帮助我吗?
I am developing an application in struts 2 and hibernate 3.
I have 3 tables
- Inspection
- InspectionMission
- 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另一种解决方案是定义一个仅用于显示这些结果的数据对象,并让 Hibernate 动态创建这些结果的实例。 这个类只需要一个匹配的构造函数。
示例类(省略 getter 和字段)
然后查询将查找
,结果您将获得
InspectionCount
的List
。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)
The query would then look
As a result you would get a
List
ofInspectionCount
s.您必须使用“新地图”语法 (Hibernate 参考第 14.6 段)
查询的其余部分是相同的。 这将返回一个映射列表,其中键是“列”的别名。
You have to use the "new map" syntax (Hibernate Reference paragraph 14.6)
The rest of the query is the same. This will return a list of maps, where the key is the alias of the "column".