PageRequest and OrderBy method name Issue

被刻印的时光 ゝ 提交于 2021-02-11 14:14:06

问题


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.

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

Repository:

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) ;

}

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:


This can have multiple reasons.

  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.

  2. 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. do value based pagination. I.e. don't select pages but select the next N rows after .

    2. 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.

    3. 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.



来源:https://stackoverflow.com/questions/61914177/pagerequest-and-orderby-method-name-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!