Skip to content
Advertisement

Get random object from SQL database via Hibernate

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:

Advertisement

Answer

Since the Criterion used in the accepted answer is deprecated, I figured out how to do it with the CriteriaBuilder & CriteriaQuery and just wanted to share it here. I used the pattern described here to extend my repository by the custom method:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement