Reputation: 26192
How does maxresult property of hibernate query works? in the example below :
Query query = session.createQuery("from MyTable");
query.setMaxResults(10);
Does this get all rows from database, but only 10 of them are displayed? or this is same as limit
in sql.
Upvotes: 7
Views: 6183
Reputation: 1
SetMaxResults retrieves all the rows and displays the number of rows that are set. In my case I didn't manage to find a method that can really retrieve only a limit number of rows. My recommendation is to create the query yourself and set there "rownum" or "limit".
Upvotes: 0
Reputation: 597046
It's the same as LIMIT
, but it is database-independent. For example MS SQL Server does not have LIMIT, so hibernate takes care of translating this. For MySQL it appends LIMIT 10
to the query.
So, always use query.setMaxResults(..)
and query.setFirstResult(..)
instead of native sql clauses.
Upvotes: 11