Java 如何将hibernate查询结果提取为list或hashmap的关联数组

Java 如何将hibernate查询结果提取为list或hashmap的关联数组,java,hibernate,struts,Java,Hibernate,Struts,我正在用struts 2和hibernate 3开发一个应用程序 我有三张桌子 检查 视察团 时间线 检查与检查任务关联,检查任务与时间线关联 现在我有以下问题。我已经在HQL中编写了以下查询 public List getQuartewiseInspectionList(){ Session session = HibernateUtil.getSessionFactory().getCurrentSession(); Query q = session.createQuery

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

我有三张桌子

  • 检查
  • 视察团
  • 时间线
  • 检查
    检查任务
    关联,
    检查任务
    时间线
    关联

    现在我有以下问题。我已经在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>
    
    
    
    这里有人能帮我吗?

    您必须使用“新地图”语法


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

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

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

    然后查询将显示

    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
    

    因此,您将获得
    检查计数的
    列表

    您好Salvatore Insalaco,谢谢您的回答。在过去的两天里,我一直在寻找这个解决方案,你的答案提供给了我。谢谢你的完美回答。这正是我所需要的,但在Hibernate文档中找不到。有人请在这个Hibernate主题上发布一个链接。
    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