Hibernate HQL 中的select new map 使用实例 嵌套Map

来源:互联网 发布:node服务器 编辑:程序博客网 时间:2024/06/11 16:10

1.关于select new map 的用法:

        String hql=“select new map(s.name as name)  from Student s”;        List ls=session.createQuery(hql).list();        for(Map m:ls){                System.out.pringln(m.get("name"));        } 

      返回的List 存放的是entry实体,用Map遍历便可得到里面的keySet和Values;

2.应用实例

public Map getEvaluatesOfItems() {String[] a =["zuzhiGongzuo","neirongZhenduixing","peixunQifen","shoukeXiaoguo","ziliaoZhiliang","renzhenChengdu","zhijiaoShuiping","biaodaNengli","jifaCanyuNengli","lianxiShijiNengli","fanyingNengli"];Map[] map =new HashMap[11];Map mm = new HashMap();for(int i=0 ; i < a.length ; i++){                    String hql = "select new map(item.${a[i]} as z,count(item.${a[i]}) as zs) from hr.training.ApplyTrainingItem item where applyTraining.id=? and item.${a[i]} is not null and item.peixunXueshi is not null group by item.${a[i]}";    List ls = ApplyTrainingItem.executeQuery(hql,[this.id]);    map[i]=new HashMap();    for(Map m:ls){map[i].put(m.get("z"),m.get("zs"));    }  mm.put(a[i],map[i]);}return mm;}
  3.    嵌套Map :

  •       首先要将每句HQL执行后List里面返回的Map放在一起,作为一个Map,(业务需求),取的是一个下拉列表的固定值,取出这些值和对应的个数,a数组中的这些都是下拉 列表获取的值存到数据库中的如下代码:
    zuzhiGongzuo(nullable:true, inList:["好","较好","一般","差","较差"])neirongZhenduixing(nullable:true, inList:["强","较强","一般","差","较差"])peixunQifen(nullable:true, inList:["好","较好","一般","差","较差"])

    ,所以要用到group by column分组函数,即取到的是不同的评价和对应的个数
  •       然后将Map数组依次放入最终返回的Map mm;这两步按照动态传参取出对应数据

0 0