我使用了以下休眠条件。
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)。我想按从(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();