Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

random rows in each query execution

 
Emrah Bektas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens if you don't include the order by at all?
 
Emrah Bektas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic