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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM