JPA原生SQL和Pageable配合报错解决

JPA原生SQL和Pageable配合报错解决

最近在做公司的项目,然后使用了JPA中的分页Pageable配合原生SQL编写分页SQL,结果一直报错

报错:

could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

修改前SQL代码:

@Query(value = "select ds.* from drugstore_seq ds where (:bussType is null or buss_type = :bussType) " +
            "",nativeQuery = true)
Page<DrugstoreSeq> findAllDrugstoreSeq(@Param(value = "bussType") Integer bussType, Pageable pageable);

出现这个错误的原因是原生SQL和Pageable编写出错了,我们写代码一般都是要求简化,业务清晰,所以我们把SQL优化一下。

修改后SQL代码:

@Query(value = "select ds from DrugstoreSeq ds where (:bussType is null or ds.bussType = :bussType) " )
Page<DrugstoreSeq> findAllDrugstoreSeq(@Param(value = "bussType") Integer bussType, Pageable pageable);

pageable处代码:PageRequest.of(page - 1, limit, Sort.Direction.DESC, “createDate”)

在这里插入图片描述

还有另外一种方法我就不讲解了,因为太麻烦了,感兴趣的可以去官网查看
问题解决

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值