PageRequest和OrderBy方法名称问题 [英] PageRequest and OrderBy method name Issue

查看:106
本文介绍了PageRequest和OrderBy方法名称问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的Spring应用程序中,我们有一个包含很多付款"记录的表.现在,我们需要一个查询,以分页从总计最大到最小的结果进行分页的查询,由于有时相同的记录包含在两个连续的页面中,因此我们面临一个错误.

in our Spring application we have a table that contains a lot of "Payment" record. Now we need a query that pages the results sorted from the one with the largest total to the smallest, we are facing an error because sometimes the same record is contained in two successive pages.

我们正在创建一个传递到存储库的PageRequest.这是我们的实现:

We are creating a PageRequest passed to the repository. Here our implementation:

存储库:

public interface StagingPaymentEntityRepository extends JpaRepository<StagingPaymentEntity, Long>  {

Page<StagingPaymentEntity> findAllByStatusAndCreatedDateLessThanEqualAndOperationTypeOrderByEffectivePaymentDesc(String status, Timestamp batchStartTimestamp, String operationType, Pageable pageable);

}

public class BatchThreadReiteroStorni extends ThreadAbstract<StagingPaymentEntity> {
PageRequest pageRequest = PageRequest.of  (index, 170);
Page<StagingPaymentEntity> records = ((StagingPaymentEntityRepository) repository).findAllByStatusAndCreatedDateLessThanEqualAndOperationTypeOrderByEffectivePaymentDesc("REITERO", batchStartTimestamp, "STORNO", pageRequest) ;

}

其中index是我们请求的页面的索引.

where index is the index of the page we are requesting.

有一种方法可以了解其发生的原因?感谢支持

There is a way to understand why it is happening ? Thank for support

推荐答案

这可能有多种原因.

  1. 非确定性排序:如果您使用的不是确定性排序,即某些行可能以任意顺序排列,顺序在选择之间可能会改变,从而导致项目被跳过或返回多次.修复:将主键作为最后一列添加到排序中.

  1. Non deterministic ordering: If the ordering you are using isn't deterministic, i.e. there are rows that might com in any order that order might change between selects resulting in items getting skipped or returned multiple times. Fix: add the primary key as a last column to the ordering.

如果以影响订购的方式更改实体,或者执行其他过程,则可能最终导致物品被多次处理.

If you change the entities in a way that affects the ordering, or another process does that you might end up with items getting processed multiple times.

在这种情况下,我看到了几种方法:

In this scenario I see a couple of approaches:

  1. 基于值的分页. IE.不要选择页面,而是选择.之后的下N行.

  1. do value based pagination. I.e. don't select pages but select the next N rows after .

代替使用Stream进行分页,这允许使用单个选择,但仍一次处理一个元素的结果.您可能需要刷新和逐出实体,但我不确定是否100%可行,但值得一试.

Instead of paging use a Stream this allows to use a single select but still processing the results an element at a time. You might have to flush and evict entities and I'm not 100% sure that works, but certainly worth a try.

最后,您可以在单独的列中标记要处理的所有所有行,然后选择N个标记的实体,并在处理它们后将其取消标记.

Finally you can mark all all rows that you want to process in a separate column, then select N marked entities and unmark them once they are processed.

这篇关于PageRequest和OrderBy方法名称问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆