What is difference between setMaxResults and setFetchSize in org.hibernate.Query?

JavaHibernate

Java Problem Overview


What is difference between setMaxResults and setFetchSize in org.hibernate.Query? I just can no get it =)

Java Solutions


Solution 1 - Java

setMaxResults is the same as LIMIT in SQL- you are setting the maximum number of rows you want returned. This is a very common use case of course.

setFetchSize is about optimization, which can change how Hibernate goes about sending the results to the caller (example: buffered, in different size chunks). setFetchSize is NOT implemented by all database drivers.

Solution 2 - Java

setMaxResults limits the number of results the query will ever get.

setFetchSize tells the jdbc driver how many rows to return in one chunk, for large queries. Say you want 1000 rows. If you set the fetch size to 100, the db will return 100, then another 100 when you want more, and so on. setFetchSize will do nothing if your driver does not support it.

Solution 3 - Java

For example, if the table has 100 records, then

criteria.setMaxResults(25);

will only fetch 25 records out of 100 records, and

criteria.setFetchSize(20);

will fetch 20 records at each time until it reads all 100 records from the table.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser590444View Question on Stackoverflow
Solution 1 - Javadev4lifeView Answer on Stackoverflow
Solution 2 - JavahvgotcodesView Answer on Stackoverflow
Solution 3 - JavaVenkata Naresh BabuView Answer on Stackoverflow