如何将hibernate查询结果作为列表或散列表的关联数组来获取 [英] How to fetch hibernate query result as associative array of list or hashmap

查看:168
本文介绍了如何将hibernate查询结果作为列表或散列表的关联数组来获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有3个表格

    $
  1. 检验

  2. InspectionMission

  3. 时间表



  4. 检验 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();

    }

    我想获取如下结果

     结果[0] [tot_inspections] =6
    结果[0] [年] =2009;
    结果[0] [quarter] =Q2;

    结果[1] [tot_inspections] =3
    结果[1] [年] =2009;
    结果[1] [quarter] =Q3;

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



    在JSP中我写了下面的代码:

     < table border =1> 

    < s:iterator value =resultstatus =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 Reference第14.6节)

     从... 

    $ b $选择新的地图(count(i.inspectionId)为tot_inspections,t.year as year,t.quarter as quarter) b

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


    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?

    解决方案

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

    这篇关于如何将hibernate查询结果作为列表或散列表的关联数组来获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆