Java – paging for updates Is it possible?
Do they have some way to handle update paging?
@Id private Integer id; @Column private boolean flag; @Column private Date last;
At the beginning, they look like: ID, false, null
I have a way to stick to it:
@Query("SELECT t FROM Test t WHERE (t.last < :processStart OR t.last IS NULL)")
Page<Test> find(Pageable pageable,@Param("processStart") Date processStart);
I need to take 10, set the flags to true and last = new date () for each and save it back to DB
Implementation looks like:
Date start = new Date();
for (int i = 0; i < 10; i++) {
Pageable request = new PageRequest(i,10,SortDirectuin.ASC,"id");
Page page = testPersistence.find(request,start);
page.getContext().forEach(t -> {
..huge part of another operation..
t.setFlag(true);
t.setLast(new Date());
testPersistence.save(t);
});
}
The home page context should be ID: 0 9; Second: 10 19;
But in my case, the second page returns ID: 20 29;
At the end of paging, I lost half of my data Return: 0 9,20.. 29,40.. 49 et al
How to prevent this?
Solution
I invented a bicycle method. I try to explain:
Pagination works as follows:
1) row1 | 2) row1 row2 | get1 row2 row3 | row3 row4 row4 | row5 row5 | get2 row6 row6 | row7 row7 row8 row8 row9 row9
But in my case, my lines were changed and they did not meet the request Pagination looks like:
1) row1 | 2) row4 row2 | get1 row5 row3 | row6 row4 row7 | row5 row8 | get2 row6 row9 | row7 row8 row9
Oh, I lost a paging cycle set
My solution is not incremental pages And get the first page all the time looks like:
1) row1 | 2) row4 | 3) row7 | row2 | get1 row5 | get1 row8 | get1 row3 | row6 | row9 | row4 row7 row5 row8 row6 row9 row7 row8 row9
In this case, I won't lose any lines But again, this is a bicycle solution that may not work in another case
