springBootJpa 联表分页查询总数不准的问题

问题情景:

   在联表查询时

    ```

// 两张表关联查询

Join<Project, Plan> planJoin =
root.join("plans", JoinType.LEFT);
predicates.add(cb.equal(
planJoin.get(ColumnConsts.SUPPLIER_ID), subjectId));
query.groupBy("id");
```
查询结果数据正确,TotalElements数量偏大。

追查源码到统计totalElement,是统计结果集的所有记录
```
private static long executeCountQuery(TypedQuery<Long> query) {

Assert.notNull(query, "TypedQuery must not be null!");

List<Long> totals = query.getResultList();
long total = 0L;

for (Long element : totals) {
total += element == null ? 0 : element;
}

return total;
}
```
解决办法:将
```
query.groupBy("id");
//换成
query.distinct(true);  
//去除重复数据即可
```
 参考文章:https://blog.csdn.net/huwentao_totti/article/details/81389882
posted @ 2019-05-09 17:33  渔阳nice  阅读(1894)  评论(0编辑  收藏  举报