我正在使用Spring Boot + Spring Batch + Spring JPA数据,并且正在尝试使用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;
}
有人可以建议我做错了吗?
答案 0 :(得分:1)
您需要向本地countQuery
添加一个@Query
参数来定义页面的大小。
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);
}
编辑