我使用Spring + Spring + Spring数据,并且尝试使用Spring中的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;
}有人能告诉我我做错了什么吗?
发布于 2018-09-08 01:42:37
您需要向本机countQuery添加一个@Query参数,以定义页面大小。
Spring数据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);
}编辑
https://stackoverflow.com/questions/52230433
复制相似问题