0

MY list size is consist 5000 records i need to get 100 records every time from list then persist to 100 records each time up to 5000 records persist to DB how can achieve this logic?

private  List<DoTempCustomers> doTempCustomers;
for(DoTempCustomers tempCustomers:doTempCustomers){
try {
    temp=new TempCustomers();               
    BeanUtils.copyProperties(temp, doTemp);
    getEntityManager().persist(temp);
}

my above code i was persist all 5000 records single for each loop.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
nag
  • 647
  • 6
  • 25
  • 43

3 Answers3

1

Try splitting the list into parts, and then do operations on each of sublists. If you need how to split list, you can refer this link: How to split array list into equal parts? which gives you idea.

Community
  • 1
  • 1
Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
  • @PradeepSimha Probably because it's a "link only" answer. Ideally we want to see a solution here in case the link goes offline – Book Of Zeus Nov 15 '18 at 02:34
0

You can use subList method of List to get the smaller view of the List

private  List<DoTempCustomers> doTempCustomers;
//initialize doTempCustomers list
List<String> muSubList = doTempCustomers.subList(0, 100);
//this will return a view of the specified range 
//(here from 0 to 99, 0 -inclusive and 100 - exclusive) within this list

Refer Javadoc for List#subList

Abubakkar
  • 15,488
  • 8
  • 55
  • 83
0

I solved same problem before some days ago. my sample code to save 1000 record each time from list rather than 500 in your case.

    public void persistData(){

    int tempTblCount = tempTblCount();     // 5000 in your case
    int loopCount = tempTblCount / 1000 + 1;
    int limit = 1;

    while (loopCount > 0) {

       // here i get list of data from temp table having query limit;
       List<MyCustomerVO> myCustomerList = getListFromTempTblData(limit);

       for (iterate over your list) {
              persist your record here;
       }
    }

    limit = limit + 1000;
    loopCount--;

   }

    public List<MyCustomerVO> getListFromTempTblData(int limit) {

    List<MyCustomerVO> getTempTblData = null;
    String sql = " select mobile_number,customer_no from temp_tbl limit "+limit+" , 1000 ";
    execute query;
    return list;

    }
NikhilK
  • 181
  • 2
  • 12