JPA @Query ERROR. token : WHERE

在使用中,发现了一个JPA的bug。

错误:ERROR. token : WHERE

 

我在Dao层,使用@Query时,使用了jpa的Pageable分页。

@Query(value = "select * from Order where refSONo=:refSONo and IsValid = 1 /*#pageable*/", nativeQuery = true)
    Page<Order> findPagedList(@Param("refSONo") String refSONo, Pageable pageable);

 

当执行时,会出现2个sql语句。

1 select * from Order where refSONo=:refSONo and IsValid = 1 外加page分页 

2 会查询总数  select count(where) from Order where refSONo=:refSONo and IsValid = 1

 

但是很奇怪,查询总数时使用了count(where),导致报错:ERROR. token : WHERE

这应该是JPA正则匹配的一个Bug。

 

解决方案:

使用 countProjection = "*",

强制count()内的字段,也可以是countProjection = "字段"

 @Query(value="select * from ad_plan plan\n" +
            "join ad_advertisement ad on plan.planID=ad.planID \n" +
            "where (ifnull(:planName,'')='' or plan.planName=:planName) \n" +
            "and (ifnull(:status,'')='' or plan.status=:status) " +
            "  /*#pageable*/ "
            ,countProjection = "*"
            ,nativeQuery = true)

 

 

where 换行

@Query(value = "select * from Order "+
" where refSONo=:refSONo and IsValid = 1 /*#pageable*/", nativeQuery = true)
    Page<Order> findPagedList(@Param("refSONo") String refSONo, Pageable pageable);

 

总结:通过换行产生的\r\n可以正确的引导正则匹配。

 

posted @ 2020-06-09 12:12  正怒月神  阅读(362)  评论(0)    收藏  举报