From bed42bf4819e1d6514aaaef10aa41b02fa54242c Mon Sep 17 00:00:00 2001 From: Glenn Renfro Date: Thu, 22 Feb 2024 18:24:43 -0500 Subject: [PATCH] Reuse CriteriaQuery when retrieving result count. (#5702) When migrating to the latest Hibernate we saw the following exception: ``` Caused by: java.lang.IllegalArgumentException: Already registered a copy: SqmBasicValuedSimplePath ``` This is because we were recreating a CriteriaQuery for our Queries. Hibernate no longer allows us to do that, but rather allows us to use the existing CriteriaQuery, but use the convenience method createCountQuery to provide the criteria query for our createQuery. Also removed 6.1.7 versionfor hibernate core that allows us to use the Boot Bom. This also caused some downstrem issues where dataflow was using the old version brought in from skipper --- .../repository/jpa/AuditRecordRepositoryImpl.java | 10 ++-------- .../repository/AppRegistrationRepositoryImpl.java | 13 ++++--------- spring-cloud-skipper/spring-cloud-skipper/pom.xml | 1 - 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/spring-cloud-dataflow-audit/src/main/java/org/springframework/cloud/dataflow/audit/repository/jpa/AuditRecordRepositoryImpl.java b/spring-cloud-dataflow-audit/src/main/java/org/springframework/cloud/dataflow/audit/repository/jpa/AuditRecordRepositoryImpl.java index 68e3123fab..42f1b80399 100644 --- a/spring-cloud-dataflow-audit/src/main/java/org/springframework/cloud/dataflow/audit/repository/jpa/AuditRecordRepositoryImpl.java +++ b/spring-cloud-dataflow-audit/src/main/java/org/springframework/cloud/dataflow/audit/repository/jpa/AuditRecordRepositoryImpl.java @@ -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; @@ -121,14 +122,7 @@ else if (fromDate != null && toDate != null) { final List resultList = typedQuery.getResultList(); - final CriteriaQuery 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); diff --git a/spring-cloud-dataflow-registry/src/main/java/org/springframework/cloud/dataflow/registry/repository/AppRegistrationRepositoryImpl.java b/spring-cloud-dataflow-registry/src/main/java/org/springframework/cloud/dataflow/registry/repository/AppRegistrationRepositoryImpl.java index 7ba4a61026..ea92c4f7b3 100644 --- a/spring-cloud-dataflow-registry/src/main/java/org/springframework/cloud/dataflow/registry/repository/AppRegistrationRepositoryImpl.java +++ b/spring-cloud-dataflow-registry/src/main/java/org/springframework/cloud/dataflow/registry/repository/AppRegistrationRepositoryImpl.java @@ -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; @@ -91,17 +92,11 @@ public Page 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 criteriaQuery = criteriaBuilder.createQuery(Long.class); - Root root = criteriaQuery.from(AppRegistration.class); - - criteriaQuery.select(criteriaBuilder.count(root)); - criteriaQuery.where(predicateArray); - - return entityManager.createQuery(criteriaQuery).getSingleResult(); + private Long getTotalCount(CriteriaQuery criteriaQuery) { + return (Long) entityManager.createQuery(((SqmSelectStatement)criteriaQuery).createCountQuery()).getSingleResult(); } } diff --git a/spring-cloud-skipper/spring-cloud-skipper/pom.xml b/spring-cloud-skipper/spring-cloud-skipper/pom.xml index 8fa123a38c..6a466cd29d 100644 --- a/spring-cloud-skipper/spring-cloud-skipper/pom.xml +++ b/spring-cloud-skipper/spring-cloud-skipper/pom.xml @@ -59,7 +59,6 @@ org.hibernate.orm hibernate-core provided - 6.1.7.Final org.zeroturnaround