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

后端 未结 15 2540
攒了一身酷
攒了一身酷 2020-11-27 09:50

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-memo

15条回答
  •  难免孤独
    2020-11-27 10:17

    Page 537 of Java Persistence with Hibernate gives a solution using ScrollableResults, but alas it's only for Hibernate.

    So it seems that using setFirstResult/setMaxResults and manual iteration really is necessary. Here's my solution using JPA:

    private List getAllModelsIterable(int offset, int max)
    {
        return entityManager.createQuery("from Model m", Model.class).setFirstResult(offset).setMaxResults(max).getResultList();
    }
    

    then, use it like this:

    private void iterateAll()
    {
        int offset = 0;
    
        List models;
        while ((models = Model.getAllModelsIterable(offset, 100)).size() > 0)
        {
            entityManager.getTransaction().begin();
            for (Model model : models)
            {
                log.info("do something with model: " + model.getId());
            }
    
            entityManager.flush();
            entityManager.clear();
            em.getTransaction().commit();
            offset += models.size();
        }
    }
    

提交回复
热议问题