简体   繁体   English

批处理10条记录Java,JPA

[英]Batch processing 10 records java , JPA

I am trying to fetch10 records at a time . 我试图一次获取10条记录。 The problem is the code just fetches first 10 records and last 4 records. 问题在于代码仅获取前10条记录和后4条记录。 It skips the in-between 10 records. 它跳过中间的10条记录。
Considering that DB has 24 records in the table. 考虑到DB在表中有24条记录。 The following program should fetch 24 records 以下程序应获取24条记录

SelectQuery: SELECT l FROM Object l where l.processed='N' fetchObjectsCount query : Select count(*) from Object obj where l.processed='N' SelectQuery:从对象l处选择l.processed ='N'fetchObjectsCount查询:从对象obj中选择count(*),其中l.processed ='N'

   private static Integer MAX_RESULT = 10;
     private DataExtraction objectExtraction(){
        int count = leadRepo.fetchObjectsCount();
        Query query = null;
        for (int i = 0; i < count; i++){
          if (i % MAX_RESULT == 0 ){
           query=entityManager.createNamedQuery("SelectQuery").setFirstResult(i).setMaxResults(MAX_RESULT);
           List<Object> tempList = (List<Object>) 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 < 1 && count - i <= MAX_RESULT){
        query = entityManager.createNamedQuery("SelectQuery").setFirstResult(i).setMaxResults(count-i);
        List<Object> tempList = (List<Object>) query.getResultList();
        entityManager.getTransaction().begin();

        for (Object ob : tempList){
        ob .setProcessed("Y");
        entityManager.persist(ob );                 
        }

        entityManager.getTransaction().commit();
      }
     }
     return this;

     }  

Given that you're fetching 10 at a time and you have 24 items in the db that code will only fetch two out of the required 3 times. 假设您一次要获取10个数据,并且数据库中有24个项目,那么该代码将仅获取所需3次中的两项。

First iteration: retrieve 10 rows, i is set to 10 第一次迭代:检索10行,我设置为10
Second iteration: retrieve 10 rows, i is set to > 10 第二次迭代:检索10行,i设置为> 10
Third iteration: won't happen 第三次迭代:不会发生

Here is how I would go about this: 这是我的处理方法:

int count = leadRepo.fetchObjectsCount();
int numberProcessed = 0;
Integer MAX_RESULT = 10;
query = entityManager.createNamedQuery("SelectQuery");


while(numberProcessed < count) {

    query.setFirstResult(numberProcessed).setMaxResults(MAX_RESULT);
    List<Object> tempList = (List<Object>) query.getResultList();
    entityManager.getTransaction().begin();

    for (Object ob : tempList) {
        ob.setProcessed("Y");
        entityManager.merge(ob);  
    }

    entityManager.getTransaction().commit();
    objectList.addAll(tempList);
    numberProcessed += tempList.size();
}

This code assumes that you want to process all rows retrieved. 此代码假定您要处理所有检索到的行。 If your named query is pulling out a different number of rows than fetchObjectsCount(); 如果您的命名查询提取的行数与fetchObjectsCount()不同; this should be written differently. 这应该用不同的方式写。

I think the problem is that you're processing the first ten records on your first iteration (because 0 % 10 == 0), then at the bottom of that loop you're incrementing i by 9. The loops starts again, incrementing i to 10, which again triggers the record processing loop. 我认为问题在于您正在处理第一次迭代中的前十条记录(因为0%10 == 0),然后在该循​​环的底部将i递增9。循环再次开始,使i递增到10,再次触发记录处理循环。 Now you're selecting the unprocessed records (only 14 now, since you've updated the first ten), skipping the first ten, and getting the last four. 现在,您要选择未处理的记录(由于更新了前十条,所以现在只有14条),跳过前十条,然后获取最后四条。 You might try starting your loop with 1 and see if that gives the results you expect. 您可以尝试从1开始循环,看看是否能得到预期的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM