首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RepositoryItemReader nativeQuery

RepositoryItemReader nativeQuery
EN

Stack Overflow用户
提问于 2018-09-07 22:46:45
回答 1查看 3.5K关注 0票数 1

我使用Spring + Spring + Spring数据,并且尝试使用Spring中的RepositoryItemReader

该批处理对于提供方法(没有nativeQuery)很好,但是我面临这个问题,尝试使用本机查询。

它总是返回一个空数组(行计数总是0)。

下面是控制台日志:

代码语言:javascript
复制
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

我的存储库方法:

代码语言:javascript
复制
@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:

代码语言:javascript
复制
@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;
}

有人能告诉我我做错了什么吗?

EN

回答 1

Stack Overflow用户

发布于 2018-09-08 01:42:37

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

来自Spring数据JPA文档

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

代码语言:javascript
复制
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);
}

编辑

Github上的最小工作实例

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52230433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档