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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/925363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:10:12  来源:igfitidea点击:

How to fetch hibernate query result as associative array of list or hashmap

javahibernatestruts

提问by amar4kintu

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

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

I have 3 tables

我有3张桌子

  1. Inspection
  2. InspectionMission
  3. Timeline
  1. 检查
  2. 检查任务
  3. 时间线

Inspectionis associated with InspectionMissionand InspectionMissionis associated with Timeline.

InspectionInspectionMissionInspectionMission相关联Timeline

Now I have following problem. I have written following query in HQL

现在我有以下问题。我在 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:

等等,以便我可以在jsp struts中显示它,如下所示:

In JSP I have written following code

在 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>

Can anyone here help me?

这里有人可以帮助我吗?

采纳答案by amar4kintu

You have to use the "new map" syntax (Hibernate Reference paragraph 14.6)

您必须使用“新地图”语法(Hibernate Reference 第 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".

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

回答by rudolfson

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.

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

Example class (getters and fields omitted)

示例类(省略了 getter 和字段)

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 Listof InspectionCounts.

结果你会得到 a Listof InspectionCounts。