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;
    }

 

posted @ 2015-07-14 10:42  243573295  阅读(881)  评论(0编辑  收藏  举报