Criteria is a simplified API for retrieving entities by composing
Criterion objects. This is a very convenient approach for functionality like "search" screens where there is a variable number of conditions to be placed upon the result set.
The
Session is a factory for
Criteria.
Criterion instances are usually obtained via the factory methods on
Restrictions. eg.
List cats = session.createCriteria(Cat.class) .add( Restrictions.like("name", "Iz%") ) .add( Restrictions.gt( "weight", new Float(minWeight) ) ) .addOrder( Order.asc("age") ) .list();
You may navigate associations using
createAlias() or
createCriteria().
List cats = session.createCriteria(Cat.class) .createCriteria("kittens") .add( Restrictions.like("name", "Iz%") ) .list();
List cats = session.createCriteria(Cat.class) .createAlias("kittens", "kit") .add( Restrictions.like("kit.name", "Iz%") ) .list();
You may specify projection and aggregation using
Projection instances obtained via the factory methods on
Projections.
List cats = session.createCriteria(Cat.class) .setProjection( Projections.projectionList() .add( Projections.rowCount() ) .add( Projections.avg("weight") ) .add( Projections.max("weight") ) .add( Projections.min("weight") ) .add( Projections.groupProperty("color") ) ) .addOrder( Order.asc("color") ) .list();
@see Session#createCriteria(java.lang.Class)
@see org.hibernate.criterion.Restrictions
@see org.hibernate.criterion.Projections
@see org.hibernate.criterion.Order
@see org.hibernate.criterion.Criterion
@see org.hibernate.criterion.Projection
@see org.hibernate.criterion.DetachedCriteria a disconnected version of this API
@author Gavin King