問題情景:
在聯表查詢時
```
// 兩張表關聯查詢
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
