zoukankan      html  css  js  c++  java
  • hibernate在使用sql查询query自动转化成model类型数据,query.addEntity

    hibernate使用自动的hql查询或者其封装的查询方法都能字段转化成对象

    而如果在hibernate中使用sql时大多返回为Object[]对象

    那么如何将object[]转换成model呢,答案就是

    addEntity(Class cls);

    实列

        public HqxgModel getMaxMinHqxg(){
            StringBuilder sb = new StringBuilder();
            sb.append("select  ");
            sb.append("'0' idStr,  ");
            sb.append("'' date,  ");
            sb.append("'0' hqsyl1End,'0' hqsyl1Start,  ");
            sb.append("'0' hqsyl2End,'0' hqsyl2Start,  ");
            sb.append("'' hqxgTitle,'' userid,  ");
            sb.append("'' industryCode,'' province,  ");
            sb.append("'' xxzrlxM,'' xxzrlxT,  ");
            //不保留小数点的
            sb.append("max(CEILING(h.hsl))hqhslEnd,min(FLOOR(h.hsl))hqhslStart, ");
            sb.append("max(CEILING(h.cjl))hqcjslEnd,min(FLOOR(h.cjl))hqcjslStart, ");
            sb.append("max(CEILING(h.cje))hqcjjeEnd,min(FLOOR(h.cje))hqcjjeStart, ");
            sb.append("max(CEILING(h.zdf))hqzdfEnd,min(FLOOR(h.zdf))hqzdfStart, ");
            sb.append("max(CEILING(h.zxj))hqzrspEnd,min(FLOOR(h.zxj))hqzrspStart, ");
            sb.append("max(CEILING(h.syl))sylEnd,min(FLOOR(h.syl))sylStart, ");
            sb.append("max(CEILING(h.mgjzc))mgjzcEnd,min(FLOOR(h.mgjzc))mgjzcStart, ");
            sb.append("max(CEILING(h.zsz))xxzszEnd,min(FLOOR(h.zsz))xxzszStart, ");
            sb.append("max(CEILING(h.zfz))fzHeJiEnd,min(FLOOR(h.zfz))fzHeJiStart, ");
            sb.append("max(CEILING(h.zzc))zczongJiEnd,min(FLOOR(h.zzc))zczongJiStart, ");
            sb.append("max(CEILING(h.zgb))xxzgbEnd,min(FLOOR(h.zgb))xxzgbStart, ");
            sb.append("max(CEILING(h.gsygpgsgdjlr))gsygpgsgdjlrEnd,min(FLOOR(h.gsygpgsgdjlr))gsygpgsgdjlrStart, ");
            sb.append("max(CEILING(h.mgsy))mgsyEnd,min(FLOOR(h.mgsy))mgsyStart, ");
            sb.append("max(CEILING(h.syl))ylnljzcsylroeEnd,min(FLOOR(h.syl))ylnljzcsylroeStart, ");
            sb.append("max(CEILING(h.srjll))ylncxljllEnd,min(FLOOR(h.srjll))ylncxljllStart, ");
            sb.append("max(CEILING(h.mll))mllEnd,min(FLOOR(h.mll))mllStart, ");
            sb.append("max(CEILING(h.jlr))jingLiRunEnd, min(FLOOR(h.jlr))jingLiRunStart, ");
            sb.append("max(CEILING(h.yysr))yingYeShouRuEnd,min(FLOOR(h.yysr))yingYeShouRuStart ");
            sb.append("from hqxg h ");
            try {
                Query query = super.getCurrentSession().createSQLQuery(sb.toString());
                List<HqxgModel> list = ((SQLQuery) query).addEntity(HqxgModel.class).list();
                if( list !=null && list.size()>0)return list.get(0);
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                //System.out.println("");
            }
            return null;
        }
  • 相关阅读:
    备忘录模式---行为型
    观察者模式(Observer)---行为型
    Hadoop基础
    centos执行-查看,复制,删除-命令的脚本
    权限问题
    12月centos单词
    配置集群遇到的问题
    SSH--完全分布式主机设置【克隆过安装过Hadoop的主机后】
    java随机排座位
    NewWord
  • 原文地址:https://www.cnblogs.com/hwaggLee/p/4644823.html
Copyright © 2011-2022 走看看