使用hibernate,实现group by and sum and count

http://blog.csdn.net/babydavic/archive/2007/11/01/1860518.aspx


   //使用hibernate,实现group by and sum and count
  Session sess = this.getSession(false);
  List list = null;
  if (sess != null) {
   Criteria cri = sess.createCriteria(getModelClass());
   cri.add(Expression.allEq(props));
   // always count id
   ProjectionList projList = Projections.projectionList();
   projList.add(Projections.sum(sum));
   projList.add(Projections.groupProperty(group1));
   projList.add(Projections.groupProperty(group2));
   projList.add(Projections.count(count));
   cri.setProjection(projList);
   list = cri.list();
  }
  list = list == null ? new ArrayList() : list;
  return list;

  //使用hibernate,实现group by and sum and count
  List listByGroupSum = dao.getListByGroupSumCP(props);
  Iterator iter = listByGroupSum.iterator();
  if (!iter.hasNext()) {
   System.out.println("No objects to display.");

  }
  while (iter.hasNext()) {
   System.out.println("New object");
   Object[] obj = (Object[]) iter.next();
   for (int i = 0; i < obj.length; i++) {
    System.out.println(obj[i]);
   }

  }
 

你可能感兴趣的:(DAO,Hibernate,.net,Blog)