如何获取 Hibernate 查询结果作为列表或哈希图的关联数组

发布于 2024-07-22 15:03:48 字数 1606 浏览 2 评论 0原文

我正在 struts 2 和 hibernate 3 中开发一个应用程序。

我有 3 个表

  1. Inspection
  2. InspectionMission
  3. Timeline

InspectionInspectionMission 关联,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

  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦行七里 2024-07-29 15:03:48

另一种解决方案是定义一个仅用于显示这些结果的数据对象,并让 Hibernate 动态创建这些结果的实例。 这个类只需要一个匹配的构造函数。

示例类(省略 getter 和字段)

public class InspectionCount() {
    // fields
    public InspectionCount(int count, int year, int quarter) {
        // initialize instance
    }
    // getters
}

然后查询将查找

select new InspectionCount(count(i.inspectionId), 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

,结果您将获得 InspectionCountList

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)

public class InspectionCount() {
    // fields
    public InspectionCount(int count, int year, int quarter) {
        // initialize instance
    }
    // getters
}

The query would then look

select new InspectionCount(count(i.inspectionId), 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

As a result you would get a List of InspectionCounts.

自我难过 2024-07-29 15:03:48

您必须使用“新地图”语法 (Hibernate 参考第 14.6 段)

select new map(count(i.inspectionId) as tot_inspections, t.year as year, t.quarter as quarter) from ...

查询的其余部分是相同的。 这将返回一个映射列表,其中键是“列”的别名。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文