Hibernate Criteria API: get n random rows

Posted by hadrien on Stack Overflow See other posts from Stack Overflow or by hadrien
Published on 2010-05-11T12:38:44Z Indexed on 2010/05/11 13:04 UTC
Read the original article Hit count: 133

Filed under:
|
|
|
|

I can't figure out how to fetch n random rows from a criteria instance:

Criteria criteria = session.createCriteria(Table.class);
criteria.add(Restrictions.eq('fieldVariable', anyValue));
...

Then what? I can't find any doc with Criteria API

Does it mean I should use HQL instead?

Thanx!

EDIT: I get the number of rows by:

int max = criteria.setProjecxtion(Projections.rowCount()).uniqueResult();

How do I fetch n random rows with indexes between 0 and max? Thx again!

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate