If you have used MySQL, at least with phpmyadmin(like me :D), then you will be familiar with the LIMIT keyword used in the SQL queries. The “LIMIT” keyword tells the query processor about the minimum number of records to be fetched for a particular query condition. If I wanted just the first 10 values from my list of mobile phones, then I would type the following query in the console “select * from mobilephones LIMIT 0,10”. This would bring 10 rows from the top of the table.
How to use LIMIT parameter in HQL?
Actually in HQL, there is no parameter or keyword called “LIMIT”. This link to the basic syntax of HQL reveals that there is no keyword called LIMIT. The LIMIT constraint is successfullly imposed by calling a function of the Query class.
//First Initialise the query object with the call to the method containing the query string.
Query query = em.createNamedQuery(“mobilephone.details”); // em is of type Entity Manager
//Now invoke the setMaxResults function on this query object. This is what it would look like:-
query.setMaxResults(10); //it takes only an integer parameter
Thats it. So that is how you limit the number of records returned by a query in HQL.
I understood this by reading this Link – https://forum.hibernate.org/viewtopic.php?p=2243712&sid=9a97d9c78515e5a9e5df17d73ded0d74