分页以进行更新.是否有可能? [英] Pagination for update. Is it possible?
问题描述
它们是否存在一些处理分页以进行更新的方法? 例如,我有100行类型:
Are they exist some methods to handle pagination for update? For example i have 100 rows of type:
@Id
private Integer id;
@Column
private boolean flag;
@Column
private Date last;
开始时,它们看起来像:id,false,null
In start they looks like: id, false, null
我有持久性方法:
@Query("SELECT t FROM Test t WHERE (t.last < :processStart OR t.last IS NULL)")
Page<Test> find(Pageable pageable, @Param("processStart") Date processStart);
我需要10个,分别设置标志true和last = new Date()并将其保存回数据库.
I need take 10, set flag true and last = new Date() for each and save it back to DB.
实施方式如下:
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);
});
}
首页上下文的ID应为:0..9; 第二:10..19;
First page context should be with id: 0..9; Second: 10..19;
但是在我的情况下,第二页返回的ID为:20..29;
But in my case second page returns with id: 20..29;
在分页结束时,我丢失了一半的数据. 返回:0..9、20..29、40..49,等等.
And in end of pagination i lost half of data. Returns: 0..9, 20..29, 40..49, etc.
如何防止这种情况?
推荐答案
我发明了一种自行车方法,并尝试对其进行解释:
I invented one bicycle method, and I try to explain it:
分页的工作方式如下:
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 rows are changed and they do not comply with the request. So pagination looks like:
1) row1 | 2) row4
row2 | get1 row5
row3 | row6
row4 row7 |
row5 row8 | get2
row6 row9 |
row7
row8
row9
糟糕,我失去了一套分页周期.
Oops, i lose one pagination cycle set.
我的解决方案不是增量页面.并始终获得首页,外观如下:
My solution is not increment page. And get first page all time It will be 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 don't lose any rows. But i repeat, it is so bicycle solution and it maybe does not work in another case.
这篇关于分页以进行更新.是否有可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!