title: Batch processing 10 records java , JPA
body: <p>I am trying to fetch10 records at a time . The problem is the code just fetches first 10 records and last 4 records. It skips the in-between 10 records.<br>
  Considering that DB has 24 records in the table. The following program should fetch 24 records</p>
  
  <p>SelectQuery: SELECT l FROM Object l where l.processed='N'
  fetchObjectsCount query : Select count(*) from Object obj where l.processed='N'</p>
  
  <pre><code>   private static Integer MAX_RESULT = 10;
       private DataExtraction objectExtraction(){
          int count = leadRepo.fetchObjectsCount();
          Query query = null;
          for (int i = 0; i &lt; count; i++){
            if (i % MAX_RESULT == 0 ){
             query=entityManager.createNamedQuery("SelectQuery").setFirstResult(i).setMaxResults(MAX_RESULT);
             List&lt;Object&gt; tempList = (List&lt;Object&gt;) query.getResultList();
             entityManager.getTransaction().begin();
  
             for (Object ob : tempList){
                ob .setProcessed("Y");
                entityManager.persist(ob );                   
               }
               entityManager.getTransaction().commit();
               i = i+9;
          }
  
          if (i % MAX_RESULT &lt; 1 &amp;&amp; count - i &lt;= MAX_RESULT){
          query = entityManager.createNamedQuery("SelectQuery").setFirstResult(i).setMaxResults(count-i);
          List&lt;Object&gt; tempList = (List&lt;Object&gt;) query.getResultList();
          entityManager.getTransaction().begin();
  
          for (Object ob : tempList){
          ob .setProcessed("Y");
          entityManager.persist(ob );                 
          }
  
          entityManager.getTransaction().commit();
        }
       }
       return this;
  
       }  
  </code></pre>