How can you remove a criterion from the criteria?

For example, if I do something like:

Criteria c = session.createCriteria(Book.class)
             .add(Expression.ge("release",reDate);
             .add(Expression.ge("price",price);
             .addOrder( Order.asc("date") )
             .setFirstResult(0)
             .setMaxResults(10);
c.list();

How can I use the same criteria instance, but delete (for example) the second criterion? I am trying to create a dynamic query in which I want the user to remove the filter without server support in order to restore the criteria from scratch.

thank

+5
source share
3 answers

, (, ..) . API , , . , , add addOrder, , , .

, , , .

, , , (, Collection), . , , , , , .

, , , , .

+6

, () ? , , , , .

, () , .

+1

:

public static void List<CriterionType> removeCriterions(Criteria criteria, Class<? extends Criterion> type) {
    Iterator<CriterionEntry> criterionIterator = ((CriteriaImpl) criteria).iterateExpressionEntries();
    while (criterionIterator.hasNext()) {
        CriterionEntry criterionEntry = criterionIterator.next();
        if (criterionEntry.getCriteria() == criteria) {
            Criterion criterion = criterionEntry.getCriterion();
            if (null == type || criterion.getClass() == type) {
                criterionIterator.remove();
            }
        }
    }
}
0
source

All Articles