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

how to get limited record in hql?

 
lokesh pattajoshi
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI every body i am using this query to get single value from data base but it is returning multiple values.I know setFirstResult and setMaxResult but except that can we use other things directly in hql.

select count(S.humanID) from Human S,Animal W WHERE S.humanID=W.animalID group by S.humanID order by count(S.humanID) desc limit 0,1
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is that limit keyword about? I don't see it in the HQL documentation.
 
Ankit Garg
Sheriff
Posts: 9579
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to use anything in the HQL query. The setFirstResult and setMaxResult method handle this nicely. Moreover the limit clause that you are referring to, is database specific. So it may work in MySql but not in Oracle or some other DBMS. So it is safer to use the method provided by Hibernate...
 
Benjamin Winterberg
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lokesh pattajoshi wrote:HI every body i am using this query to get single value from data base but it is returning multiple values.I know setFirstResult and setMaxResult but except that can we use other things directly in hql.

select count(S.humanID) from Human S,Animal W WHERE S.humanID=W.animalID group by S.humanID order by count(S.humanID) desc limit 0,1


There is no database independant way to express the FirstResult/MaxResult semantic in SQL. So there is no way you can do it via HQL. setFirstResult/setMaxResults will be translated into LIMIT x,y for MySQL Dialect. But the sql for other databases is totally different (e.g. "fetch first x rows only" for DB2).

If you except a single row result but you get mulitple rows than you should re-think your statement. It might be wrong.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic