1

I am writing a batch and using JPA for persistence. I have a table with a lot of records and I need to read all the records by groups of 100 or 500. I don't want to load all the records into memory at once, because there won't be enough memory.

For example:

  • First group: records 1 to 500
  • Second group: records 501 to 1000
  • etc

Is there a better way to do it?


Related questions

JPA: what is the proper pattern for iterating over large result sets?

Total row count for pagination using JPA Criteria API

Community
  • 1
  • 1
Vitaly Olegovitch
  • 3,509
  • 6
  • 33
  • 49

1 Answers1

2

You should set the first result and the number of results in your javax.persistence.Query with methods setFirstResult() and setMaxResults().

Fortunato
  • 1,360
  • 10
  • 9
  • Thank you! I used a variable to use the first result number, initialised to 0. Then I setted the number of max results, and then I made the interrogation to the DB using the query() method. Last thing to do was incrementing the first result by the number of max results of the previous query. – Vitaly Olegovitch Mar 29 '12 at 23:48