In Hibernate 3, is there a way to do the equivalent of the following MySQL limit in HQL?
select * from a_table order by a_table_column desc limit 0, 20;
I don't want to use setMaxResults if possible. This definitely was possible in the older version of Hibernate/HQL, but it seems to have disappeared.
String hql = "select userName from AccountInfo order by points desc 5";
This worked for me without using
setmaxResults();
Just provide the max value in the last (in this case 5) without using the keyword
limit
. :PMy observation is that even you have limit in the HQL (hibernate 3.x), it will be either causing parsing error or just ignored. (if you have order by + desc/asc before limit, it will be ignored, if you don't have desc/asc before limit, it will cause parsing error)
If you don't want to use
setMaxResults()
on theQuery
object then you could always revert back to using normal SQL.If you don't want to use setMaxResults, you can also use Query.scroll instead of list, and fetch the rows you desire. Useful for paging for instance.
You need to write a native query, refer this.