计入 Spring 数据 JPQL 连接查询抛出 IllegalArgumentException

Count in Spring Data JPQL join query throwing IllegalArgumentException

我在 Spring 数据中的注释 @Query 中使用 JPQL 进行查询,如下所示

@Query(value = "select distinct a from EntityA as a left join fetch a.listEntityB as b where ...")
  public List<EntityA > find....(@Param("") String value, Pageable pageable);

现在我需要知道查询的记录总数。由于 Pageable 给我部分列表,我正在编写另一个没有 Pageable 的计数查询。我的计数查询如下所示:

@Query(value = "select count(distinct a) from EntityA as a left join fetch a.listEntityB as b where ...) 
  public long count...(@Param("") String value);

但是,我从该查询中收到“IllegalArgumentException:查询验证失败”错误以计算记录。当我将计数查询更改为 "select count(distinct a) from EntityA" 时,它工作正常。我不确定使用连接时出现了什么问题。到目前为止,我没有得到任何有用的文件。有没有更好的解决方案来获取Pageable查询的记录总数。

将 CountQuery 放在与查询本身相同的注释中。所以

@Query(value = "select ...", countQuery = "select count..")
public List<EntityA > find....(@Param("") String value, Pageable pageable);

同时从 countQuery 中删除“fetch”。

要获取总记录数,只需参考page.getTotalElements()到return的总记录数即可。