本文介绍了如何在hibernate投影结果上应用顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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();
I used following hibernate criteria.
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();
This returns a list of objects and the objects have two fields, studentId and count(studentId). I want to sort this list by count(studentId) descending. How do I do this hibernate?
currently I am getting
{[1, 5], [2, 7], [3,4]}
I want to get it this
{[2, 7], [1, 5], [3,4]}
solution:this sloves the issue :)
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();
解决方案
Since there is no answer accepted - i formatted and simplified your own solution a bit:
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();
这篇关于如何在hibernate投影结果上应用顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!