JPA: what is the proper pattern for iterating over large result sets?
- by Caffeine Coma
Let's say I have a table with millions of rows. Using JPA, what's the proper way to iterate over a query against that table, such that I don't have all an in-memory List with millions of objects?
I suspect that the following will blow up if the table is large:
List<Model> models = entityManager().createQuery("from Model m", Model.class).getResultList();
for (Model model : models)
{
// do something with model
}
Is pagination (looping and manually updating setFirstResult()/setMaxResult()) really the best solution?