Announcement Announcement Module
No announcement yet.
hibernate - select random rows. Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • hibernate - select random rows.

    I am using both hibernate and spring in my application.

    I am looking for a way to query the database and return a few random entries from a table. For example, suppose i ahve a table called 'user'. I want to be able to retrieve 3 users at random from that table. How can this be accomplished using hibernate?

    I have looking into the HQL language but could not find anythign that could help me. The best i can think of is retrieving the max(id) and min(id), have a random number generator between max and min, and use the get(id) command.

    Your help is appreciated. What is the best way I can accomplish this. Thank you.

  • #2
    I have to confess I've not seen this requirement before. You can obviously select the IDs and then a random few to use.
    Last edited by karldmoore; Aug 27th, 2007, 04:06 PM.


    • #3
      lol, leave it to me to be unusual with my requirements. thanks for the response.