Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Reuse CriteriaQuery when retrieving result count. #5702

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;

import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
import org.springframework.cloud.dataflow.audit.repository.AuditRecordRepositoryCustom;
import org.springframework.cloud.dataflow.core.AuditActionType;
import org.springframework.cloud.dataflow.core.AuditOperationType;
Expand Down Expand Up @@ -121,14 +122,7 @@ else if (fromDate != null && toDate != null) {

final List<AuditRecord> resultList = typedQuery.getResultList();

final CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
countQuery.select(cb.count(countQuery.from(AuditRecord.class)));

if (!finalQueryPredicates.isEmpty()) {
countQuery.where(finalQueryPredicates.toArray(new Predicate[0]));
}

final Long totalCount = entityManager.createQuery(countQuery)
final Long totalCount = (Long)entityManager.createQuery(((SqmSelectStatement)select).createCountQuery())
.getSingleResult();

return new PageImpl<>(resultList, pageable, totalCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;

import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.dataflow.core.AppRegistration;
Expand Down Expand Up @@ -91,17 +92,11 @@ public Page<AppRegistration> findAllByTypeAndNameIsLikeAndVersionAndDefaultVersi
appRegistration.setVersions(versions);
});
}
return new PageImpl<>(resultList, pageable, getTotalCount(cb, predicates.toArray(new Predicate[0])));
return new PageImpl<>(resultList, pageable, getTotalCount(cq));
}

private Long getTotalCount(CriteriaBuilder criteriaBuilder, Predicate[] predicateArray) {
CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
Root<AppRegistration> root = criteriaQuery.from(AppRegistration.class);

criteriaQuery.select(criteriaBuilder.count(root));
criteriaQuery.where(predicateArray);

return entityManager.createQuery(criteriaQuery).getSingleResult();
private Long getTotalCount(CriteriaQuery<AppRegistration> criteriaQuery) {
return (Long) entityManager.createQuery(((SqmSelectStatement)criteriaQuery).createCountQuery()).getSingleResult();
}

}
1 change: 0 additions & 1 deletion spring-cloud-skipper/spring-cloud-skipper/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<scope>provided</scope>
<version>6.1.7.Final</version>
</dependency>
<dependency>
<groupId>org.zeroturnaround</groupId>
Expand Down