Add a special query logic with group by and count
This commit is contained in:
parent
e8366aa61a
commit
35c4ec821e
|
@ -164,8 +164,16 @@ public class QueryableHibernateList<T extends DataEntity> implements QueryableLi
|
||||||
if (distinct) criteriaQuery.select(criteriaBuilder.countDistinct(this.root.get("id")));
|
if (distinct) criteriaQuery.select(criteriaBuilder.countDistinct(this.root.get("id")));
|
||||||
else criteriaQuery.select(criteriaBuilder.count(this.root));
|
else criteriaQuery.select(criteriaBuilder.count(this.root));
|
||||||
criteriaQuery.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
criteriaQuery.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
||||||
|
if (!this.groupings.isEmpty()) criteriaQuery.groupBy(this.generateGroupPredicates(this.groupings, this.root));
|
||||||
//if (distinct) criteriaQuery.distinct(true);
|
//if (distinct) criteriaQuery.distinct(true);
|
||||||
|
|
||||||
|
//GK: Group By special case. When group by is used, since it will result in a list of how many elements have the grouped field common
|
||||||
|
// then it will instead return the number of the distinct values of the grouped field
|
||||||
|
if (this.groupings.isEmpty()) {
|
||||||
return this.manager.createQuery(criteriaQuery).getSingleResult();
|
return this.manager.createQuery(criteriaQuery).getSingleResult();
|
||||||
|
} else {
|
||||||
|
return (long) this.manager.createQuery(criteriaQuery).getResultList().size();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Async
|
@Async
|
||||||
|
@ -176,8 +184,15 @@ public class QueryableHibernateList<T extends DataEntity> implements QueryableLi
|
||||||
if (distinct) criteriaQuery.select(criteriaBuilder.countDistinct(this.root.get("id")));
|
if (distinct) criteriaQuery.select(criteriaBuilder.countDistinct(this.root.get("id")));
|
||||||
else criteriaQuery.select(criteriaBuilder.count(this.root));
|
else criteriaQuery.select(criteriaBuilder.count(this.root));
|
||||||
criteriaQuery.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
criteriaQuery.where(this.generateWherePredicates(this.singlePredicates, this.root, this.nestedPredicates, this.nestedQueryRoot));
|
||||||
|
if (!this.groupings.isEmpty()) criteriaQuery.groupBy(this.generateGroupPredicates(this.groupings, this.root));
|
||||||
//if (distinct) criteriaQuery.distinct(true);
|
//if (distinct) criteriaQuery.distinct(true);
|
||||||
return CompletableFuture.supplyAsync(() -> this.manager.createQuery(criteriaQuery).getSingleResult());
|
return CompletableFuture.supplyAsync(() -> {
|
||||||
|
if (this.groupings.isEmpty()) {
|
||||||
|
return this.manager.createQuery(criteriaQuery).getSingleResult();
|
||||||
|
} else {
|
||||||
|
return (long) this.manager.createQuery(criteriaQuery).getResultList().size();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue