Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bulk reading of data in hibernate using scrollable result set

I was reading a blog regarding bulk fetching with hibernate http://java.dzone.com/articles/bulk-fetching-hibernate.
In this, ScrollableResults is used as a solution. Here still we need to evict objects from session.
I don't understand how using ScrollableResults(or scroll()) is different from using list().

In other words, how the below statements differ in terms of performance

 List<Employee> empList = session.createCriteria(Employee.class).list();
 ScrollableResults sc = session.createCriteria(Employee.class).scroll();

Please let me know.

like image 986
Anand Avatar asked Oct 23 '25 12:10

Anand


1 Answers

The above code seems to be missing some settings.

Query query = session.createQuery(query);
query.setReadOnly(true);
// MIN_VALUE gives hint to JDBC driver to stream results
query.setFetchSize(Integer.MIN_VALUE);
ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);
// iterate over results
while (results.next()) {
    Object row = results.get();
    // process row then release reference
    // you may need to flush() as well
}
results.close();

Follow this link for more details and explanation.

like image 179
Andy Dufresne Avatar answered Oct 25 '25 01:10

Andy Dufresne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!