views:

141

answers:

5

If I want to fetch million rows in hibernate, how would it work? Will hibernate crash? How can I optimize that.

+3  A: 

If you read one/write one it will probably work fine. Are you sure this the way you want to read 1,000,000 rows? It will likely take a while.

If you want all the objects to be in memory at the same time, you might well be challenged.

You can optimize it best, probably, by finding a different way. For example, you can dump from the database using database tools much more quickly than reading with hibernate.

You can select sums, maxes, and counts in the database without returning a million rows over the network.

What are you trying to accomplish, exactly?

Tony Ennis
+1  A: 

Bring only the columns you need. Try it out in a test environment.

p.campbell
+5  A: 

typically you wouldn't use hibernate for this. If you need to do a batch operation, use sql or the hibernate wrappers for batch operations. There is no way loading millions of records is going to end well for your application. Your app with thrash as the gc runs, or possibly crash. there has to be another option.

hvgotcodes
+1  A: 

For this you would be better off using spring's jdbc tools with a row handler. It will run the query and then perform some action on a row at a time.

Steven
+1  A: 

You should try looking at the StatelessSession interface and example of which can be found here:

http://mrmcgeek.blogspot.com/2010/09/bulk-data-loading-with-hibernate.html

Andy Mc