Results 1 to 3 of 3

Thread: hibernate - select random rows.

  1. #1

    Default 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. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    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 at 04:06 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  3. #3

    Default

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •