本文介绍了动态移除org.hibernate.Criteria中结果集的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Criteria criteria = session.createCriteria(Libro.class).addOrder(Order.asc (ID) );
但是,当我想要获得rowcount失败时:
criteria.setProjection(Projections.rowCount());
因为查询中有一个订单。
如何动态地移除Criteria中的排序?
我的意思是,我正在寻找像 criteria.removeOrder(ID )
。
解决方案
大多数Criteria实际上是CriteriaImpl的实例。如果您将Criteria转换为CriteriaImpl并获取订单的迭代器,则可以这样删除它们。
Criteria criteria = session。 createCriteria(Libro.class).addOrder(Order.asc(ID));
迭代器<顺序> orderIter =((CriteriaImpl)条件).iterateOrderings();
while(orderIter.hasNext()){
orderIter.next();
orderIter.remove();
}
long totalRows =(Long)criteria.setProjection(Projections.rowCount())。uniqueResult();
I have a Criteria with:
Criteria criteria= session.createCriteria(Libro.class).addOrder( Order.asc("ID") );
However, when I want to get the rowcount fail:
criteria.setProjection(Projections.rowCount());
because there is an order by in the query.
How to remove dynamically the ordering in the Criteria?
I mean, I am looking for like criteria.removeOrder("ID")
.
解决方案
Most Criteria are actually instances of CriteriaImpl. If you cast your Criteria to CriteriaImpl and get the iterator for orders, you can remove them that way.
Criteria criteria= session.createCriteria(Libro.class).addOrder( Order.asc("ID") );
Iterator<Order> orderIter = ((CriteriaImpl)criteria).iterateOrderings();
while (orderIter.hasNext()) {
orderIter.next();
orderIter.remove();
}
Long totalRows = (Long)criteria.setProjection(Projections.rowCount()).uniqueResult();
这篇关于动态移除org.hibernate.Criteria中结果集的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!