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