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。

解决方案:where 换行

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

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

原文地址:https://www.cnblogs.com/hanjun0612/p/13071790.html