RepositoryItemReader nativeQuery

问题描述 投票:1回答:1

我正在使用Spring Boot + Spring Batch + Spring JPA Data,我正在尝试使用Spring Batch的RepositoryItemReader

批处理对于给定方法(没有nativeQuery)工作正常,但我正面临这个问题,尝试使用本机查询。

问题始终是返回一个空数组(行数始终为0)。

以下是控制台日志:

Hibernate: SELECT * FROM QUOTE_OFFER_FULFILLMENT QOF WHERE QOF.STATUS=? OR QOF.STATUS=?  
#pageable
 order by QOF.id desc limit ?
[DEBUG] org.hibernate.loader.Loader - bindNamedParameters() FULFILLMENT_READY -> 1 [1]
[DEBUG] org.hibernate.loader.Loader - bindNamedParameters() FAILED -> 2 [2]
[DEBUG] org.hibernate.stat.internal.ConcurrentStatisticsImpl - HHH000117: HQL: SELECT * FROM QUOTE_OFFER_FULFILLMENT QOF WHERE QOF.STATUS=? OR QOF.STATUS=?  
#pageable
 order by QOF.id desc, time: 1ms, rows: 0
[DEBUG] org.hibernate.engine.transaction.internal.TransactionImpl - committing

我的存储库方法:

@Component
@Repository
public interface QuoteOfferFulfillmentRepository
        extends JpaRepository<QuoteOfferFulfillment, Long> {
@query(value = "SELECT * FROM QUOTE_OFFER_FULFILLMENT QOF WHERE QOF.STATUS=?1 OR QOF.STATUS=?2 \n#pageable\n", nativeQuery = true)
Page findTempByStatus(QuoteOfferFulfillmentStatus status,
QuoteOfferFulfillmentStatus status1, Pageable pageable);
}

和我的BatchConfiguration:

@Bean 
public RepositoryItemReader reader() {
    RepositoryItemReader fullfillment = new RepositoryItemReader();
    fullfillment.setRepository(fulfillmentRepository);
    fullfillment.setMethodName("findTempByStatus");
    List list = new ArrayList();
    list.add(QuoteOfferFulfillmentStatus.FULFILLMENT_READY);
    list.add(QuoteOfferFulfillmentStatus.FAILED);
    fullfillment.setPageSize(40);
    fullfillment.setArguments(list);
    HashMap<String, Direction> sorts = new HashMap<>();
    sorts.put("id", Direction.DESC);
    fullfillment.setSort(sorts);
    return fullfillment;
}

任何人都可以建议我做错了什么?

spring spring-boot spring-batch
1个回答
1
投票

您需要向本机countQuery添加@Query参数以定义页面的大小。

来自Spring Data JPA docs

Spring Data JPA目前不支持对本机查询进行动态排序,因为它必须操纵声明的实际查询,而对于本机SQL,它无法可靠地执行。但是,您可以通过自己指定计数查询来使用本机查询进行分页,如以下示例所示:

public interface UserRepository extends JpaRepository<User, Long> {

  @Query(value = "SELECT * FROM USERS WHERE LASTNAME = ?1",
    countQuery = "SELECT count(*) FROM USERS WHERE LASTNAME = ?1",
    nativeQuery = true)
  Page<User> findByLastname(String lastname, Pageable pageable);
}

编辑

Minimal working example on Github

© www.soinside.com 2019 - 2024. All rights reserved.