I have a method called getAllEmployees() which returns a pageable.
Lets say the page size is 1 and started with page 0
Pageable pageable = PageRequest.of(0, 1);
Page<String> allEmployees = service.getAllEmployeeNames(pageable)
while(true) {
for(String name: allEmployees.getContent()) {
// check whether the employee needs to be deleted or not based on certain conditions
boolean isDelete = service.checkEmployeeToBeDeleted()
if (isDelete)
EmployeeEntity entity = service.findByName(name);
service.delete(entity);
}
if (!page.hasNext()) {
break;
}
pageable = page.nextPageable();
}
- In this scenario, all employees are not deleted only those matching the condition will be deleted
as page size is 1
Let's say, total 6 employees
emp pageable(pageNumber, pageSize)
1 (0 ,1)
2 (1 ,1)
3 (2 ,1)
4 (3 ,1)
5 (4 ,1)
6 (5 ,1)
when 1 gets deleted the pageable will be like
2 (0 ,1)
3 (1 ,1)
4 (2 ,1)
5 (3 ,1)
6 (4 ,1)
but as we go with page.nextPageable() the next one will be like (1,1)
So on next fetch, it will pick emp 3, and emp 2 will be missed