Hibernate DetachedCriteria is not reusable

Once you execute following code:

Criteria criteria = detachedCriteria

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:


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

For more info see Hibernate forum –

Leave a Reply

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