aspose file tools
The moose likes Object Relational Mapping and the fly likes random rows in each query execution Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "random rows in each query execution" Watch "random rows in each query execution" New topic
Author

random rows in each query execution

Emrah Bektas
Greenhorn

Joined: Jan 19, 2010
Posts: 3
if i say "order by ... desc" in query q, then it returns first 5 rows (q.setMaxResult(5))

if i don't define "order by" in query q, then returns last 5 rows (q.setMaxResult(5))

Question:
I wanna get random 5 rows in each execution.But i couldn't find any solution.
How do i resolve this problem?
Thanks...

Fo example in MySQL "order by rand()"
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4967

What happens if you don't include the order by at all?


Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
Emrah Bektas
Greenhorn

Joined: Jan 19, 2010
Posts: 3
Hello Cameron,
As i said if i don't include "order by",then last 5 data are returned.
I wanna get random 5 data in each running of select statement

Code piece

sessionh = HibernateUtil.getSessionFactory().getCurrentSession();
Query q = sessionh.createQuery("Select msg from StatusMessageSuggest msg");
q.setMaxResults(5);

Thanks..
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Which rows are returned will depend on the database. You'd have to check the docs specific to the DB you're using, but my guess is it'll all be DB-specific, and you'd need a custom query to do it with an ORM framework.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: random rows in each query execution
 
Similar Threads
Vectors - please explain
limiting collection result size
Solved : JTDS MSSQL Pagination problem.
ResultSet TYPE_FORWARD_ONLY
Selecting random rows