-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Hibernate Criteria return rows in random order
PostPosted: Wed Aug 20, 2008 1:26 pm 
Newbie

Joined: Wed Aug 20, 2008 1:16 pm
Posts: 1
I am trying to return a query with the rows in random order using a hibernate criteria query. a fixed number of rows needs to be returned.

_________________
Leefa


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 20, 2008 4:56 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Returning a fixed number of rows should be rather easy. Use the Criteria.setFirstResult() and Criteria.setMaxResults() methods.

Sorting in random order may be a bit more complicated. The only thing I can think of is to subclass the org.hibernate.criterion.Order class and implement the toSqlString() method to return something that generates:

Code:
... order by rand()


This will of course only work if your database supports it. Take a look at the MySQL documentation for an example: http://dev.mysql.com/doc/refman/5.0/en/ ... ction_rand


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 19, 2008 9:46 pm 
Is it possible to use crit.addOrder() to add get rand(),
im using postgres. ?

where postgres support that, according to
http://www.petefreitag.com/item/466.cfm
but i want to use within Criteria.
Please help me out..


Top
  
 
 Post subject:
PostPosted: Sat Oct 25, 2008 12:21 pm 
Newbie

Joined: Mon Apr 14, 2008 5:34 am
Posts: 2
Location: Poland
Had the same problem :)
See http://opensource.atlassian.com/projects/hibernate/browse/HHH-2381 for a solution.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.