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; }
知识只有共享才能传播,才能推崇出新的知识,才能学到更多,这里写的每一篇文字/博客,基本都是从网上查询了一下资料然后记录下来,也有些是原滋原味搬了过来,也有时加了一些自己的想法