【问题标题】:RepositoryItemReader nativeQueryRepositoryItemReader nativeQuery
【发布时间】:2018-09-07 22:46:45
【问题描述】:

我正在使用 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】:

    你需要给你的原生@Query添加一个countQuery参数来定义你的页面大小。

    来自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

    【讨论】:

    • 嗨@gustavo Passini,感谢您的回复。根据您的以下建议进行更改,@Query(value = "SELECT * FROM QUOTE_OFFER_FULFILLMENT QOF WHERE QOF.STATUS=?1 OR QOF.STATUS=?2 \n#pageable\n", countQuery = "SELECT count(*) FROM offer.QUOTE_OFFER_FULFILLMENT QOF WHERE QOF.STATUS=?1 OR QOF.STATUS=?2", nativeQuery = true) Page findTempByStatus(QuoteOfferFulfillmentStatus status, QuoteOfferFulfillmentStatus status1, Pageable pageable);但没有运气。结果是一样的。结果为0。我做错了什么吗??
    • 你好@Balakrishna Tirumalasetti。不客气。抱歉,我现在看不出问题出在哪里。我已将一个最小的工作示例上传到我的 Github。希望能帮到你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-16
    • 1970-01-01
    • 2020-10-04
    相关资源
    最近更新 更多