While working with the Criteria API maybe you noticed that there is no method for getting values in random order. But there is a very easy workaround.
It is just important that you add a Restriction which does nothing, but to which you can append your order by rand()
expression. Note that it is important that you insert the order by
as the last Restriction.
Criteria criteria = session.createCriteria(SomeClass.class);
criteria.add(Restrictions.eq('someVariable', someValue));
criteria.add(Restrictions.sqlRestriction("1=1 order by rand()"));
criteria.setMaxResults(10);
return criteria.list();