0

I have a requirement where I have to pull a random set of records from a MySQL table and this can be achieved by using the RAND() method inside the SQL query as shown below:

SELECT Id
FROM table1
ORDER BY RAND()

However I need to implement the same thing now using the Ebean ORM and after reading through the docs I could not find any reference to the Rand() method. Is there an equivalent in Ebean to this? Or anyway I can replicate the same functionality purely through Ebean only?

Available methods for the orderBy query in Ebean: https://ebean.io/docs/query/orderBy

  • Does this answer your question? [MySQL select 10 random rows from 600K rows fast](https://stackoverflow.com/questions/4329396/mysql-select-10-random-rows-from-600k-rows-fast) – Aaron Morefield Jul 11 '20 at 05:34
  • Not really as this involves writing a raw sql query but I am looking for a way to directly achieve this by calling a method within the Ebean ORM. – MarbleBeach Jul 11 '20 at 05:41

1 Answers1

0

You would use an orderBy expression:

.orderBy("RAND()")
chaos
  • 122,029
  • 33
  • 303
  • 309