关于使用jpa聚合函数遇到的问题

  private List getCountByStatusOrSource(Integer status, Integer source, Integer userId, String startTime, String endTime, Integer timeSlot, String type) throws Exception {

  CriteriaBuilder cb = entityManager.getCriteriaBuilder();

  Date sTime = new Date(), eTime = new Date();

  CriteriaQuery query = cb.createTupleQuery();

  Root root = query.from(AAA.class);//AAA是对应数据库的类名,替换成自己的

  Path statusPath = root.get("status");

  Path statusNamePath = root.get("status").get("name");

  Path sourcePath = root.get("source");

  Path operatorPath = root.get("operator");

  List predicateList = new ArrayList<>();

  if (source != null) {

  predicateList.add(

  cb.equal(sourcePath, source)

  );

  }

  if (userId != null) {

  predicateList.add(

  cb.equal(operatorPath, userId)

  );

  }

  Map timeMap = getChangedTime(startTime, endTime, timeSlot);//获取时间的方法,具体代码我就不沾了,自己写个就行了

  sTime = (Date) timeMap.get("sTime");

  eTime = (Date) timeMap.get("eTime");

  Expression startDateExpression = cb.literal(sTime);

  Expression endDateExpression = cb.literal(eTime);

  predicateList.add(

  cb.between(updateTimePath, startDateExpression, endDateExpression)

  );

  Predicate[] predicates = new Predicate[predicateList.size()];

  predicates = predicateList.toArray(predicates);

  query.where(predicates);//where条件加上

  if ("status".equals(type)) {

  query.select(cb.tuple(statusPath, cb.count(root)));

  query.groupBy(statusPath);

  }

  //query.multiselect(statusPath, cb.count(root));//

  TypedQuery q = entityManager.createQuery(query);

  List result = q.getResultList();

  return result;

  }