Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do select the data in batches using hibernate?

I have a function which basically returns the entire data from the table.How can i implement batch fetching so that the data will be returned in batches of 60,000 rows at a time.

Will the following logic applicable in this scenario?

http://javainnovations.blogspot.com/2008/07/batch-insertion-in-hibernate.html

like image 302
ajithparamban Avatar asked Oct 24 '25 01:10

ajithparamban


1 Answers

To select data in a batch, you can apply data pagination by setting the initial position & the number of results to be fetched for a query.

for(int i=0; i < MAX_SIZE; i = i + BATCH_SIZE){

    List<Object> resultList = entityManager.createQuery(SQL_QUERY).setFirstResult(i).setMaxResults(BATCH_SIZE).getResultList();

    //-- Batch Computation
} 

I have provided sample code, can modify it accordingly.

like image 127
Nayan Wadekar Avatar answered Oct 25 '25 13:10

Nayan Wadekar