java-如何在休眠投影结果上应用命令

提问

我使用了以下休眠标准.

Criteria criteria = session.createCriteria(Student.class);
        ProjectionList projectionList = Projections.projectionList();
       projectionList.add(Projections.groupProperty("studentId"));
       projectionList.add(Projections.count("studentId"));
        criteria.setProjection(projectionList);
      return criteria.list();

这将返回一个对象列表,并且该对象具有两个字段,studentId和count(studentId).我想按count(studentId)降序对该列表进行排序.我该如何休眠?

目前我正在

{[1, 5], [2, 7], [3,4]} 

我想要这个

{[2, 7], [1, 5], [3,4]}

解:
这解决了这个问题:)

  Criteria criteria = session.createCriteria(Student.class);
            ProjectionList projectionList = Projections.projectionList();
           projectionList.add(Projections.groupProperty("studentId"));
           projectionList.add( Projections.alias(Projections.count("studentId"),"count"));
            criteria.setProjection(projectionList);
           criteria.addOrder(Order.desc(("count")));
            return criteria.list();

最佳答案

由于没有答案被接受-我格式化并简化了自己的解决方案:

Criteria criteria = session.createCriteria(Student.class);
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.groupProperty("studentId"));
projectionList.add(Projections.count("studentId"), "count");
criteria.setProjection(projectionList);
criteria.addOrder(Order.desc(("count")));
return criteria.list();