I have following MySql dependent code ( ORDER BY RAND() ) . I would like to know if there is hibernate HQL alternative for it (admin is boolean tag indicating that the user as an admin). This is working code:
public long getRandomAdmin() {
Session session = getSession();
Query selectQuery = session.createSQLQuery("SELECT user_id FROM users WHERE admin = '1' ORDER BY RAND()");
selectQuery.setMaxResults(1);
List<BigInteger> list = null;
try {
list = selectQuery.list();
} catch (HibernateException e) {
log.error(e);
throw SessionFactoryUtils.convertHibernateAccessException(e);
}
if (list.size() != 1) {
log.debug("getRandomAdmin didn't find any user");
return 0;
}
log.debug("found: " + list.get(0));
return list.get(0).longValue();
}