Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Criteria Limit mechanism?

Hibernate Criteria support provides a setMaxResults() method to limit the results returned from the db.

I can't find any answer to this in their documentation - how is this implemented? Is it querying for the entire result set and then returning only the request number? Or is it truly limiting the query on the database end (think LIMIT keyword as in mySql).

This is important because if a query could potentially return many many results, I really need to know if the setMaxResults() will still query for all the rows in the database (which would be bad).

Also - if its truly limiting the number of rows on the database end, how is it achieving this cross-db (since I don't think every rdbms supports a LIMIT functionality like mySql does).

like image 723
john Avatar asked Aug 31 '11 22:08

john


People also ask

What is restriction in Hibernate criteria?

1.2. The Restriction class in hibernate provide several methods that can be used as conditions (also known as Criterion). These conditions are added to a criteria object with the add() method. This method takes an org. hibernate.

How do I give a limit in HQL?

Below snippet is used to perform limit query using HQL. Query query = session. createQuery("...."); query. setFirstResult(startPosition); query.


1 Answers

Hibernate asks the database to limit the results returned by the query. It does this via the dialect, which uses whatever database-specific mechanism there is to do this (so for SQL Server it will do somthing like "select top n * from table", Oracle will do "select * from table where rownum < n", MySQL will do "select * from table limit n" etc). Then it just returns what the database returns.

like image 116
Ritesh Mengji Avatar answered Oct 10 '22 04:10

Ritesh Mengji