Hibernate DetachedCriteria is not reusable

Once you execute following code:

Criteria criteria = detachedCriteria
.getExecutableCriteria(session);

you cannot reause the detachedCriteria for creating another one executable criteria because they are connected together (e.g. setting projection or pagination for criteria modifies also detachedCriteria object).

You must do following workarounds:

criteria.setMaxResults(0);
criteria.setFirstResult(0);

detachedCriteria.setResultTransformer( Criteria.ROOT_ENTITY );
detachedCriteria.setProjection( null );

For more info see Hibernate forum –
http://forum.hibernate.org/viewtopic.php?t=939308

Leave a Reply

Your email address will not be published. Required fields are marked *