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.
How to persist very 100 record from List
Question
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.
Solution
OTHER TIPS
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
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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow