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

Commit bed42bf

Browse files
authored
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
1 parent 32fc5b7 commit bed42bf

File tree

3 files changed

+6
-18
lines changed

3 files changed

+6
-18
lines changed

spring-cloud-dataflow-audit/src/main/java/org/springframework/cloud/dataflow/audit/repository/jpa/AuditRecordRepositoryImpl.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import jakarta.persistence.criteria.Predicate;
2929
import jakarta.persistence.criteria.Root;
3030

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

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

124-
final CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
125-
countQuery.select(cb.count(countQuery.from(AuditRecord.class)));
126-
127-
if (!finalQueryPredicates.isEmpty()) {
128-
countQuery.where(finalQueryPredicates.toArray(new Predicate[0]));
129-
}
130-
131-
final Long totalCount = entityManager.createQuery(countQuery)
125+
final Long totalCount = (Long)entityManager.createQuery(((SqmSelectStatement)select).createCountQuery())
132126
.getSingleResult();
133127

134128
return new PageImpl<>(resultList, pageable, totalCount);

spring-cloud-dataflow-registry/src/main/java/org/springframework/cloud/dataflow/registry/repository/AppRegistrationRepositoryImpl.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import jakarta.persistence.criteria.Predicate;
2929
import jakarta.persistence.criteria.Root;
3030

31+
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
3132
import org.springframework.beans.factory.ObjectProvider;
3233
import org.springframework.beans.factory.annotation.Autowired;
3334
import org.springframework.cloud.dataflow.core.AppRegistration;
@@ -91,17 +92,11 @@ public Page<AppRegistration> findAllByTypeAndNameIsLikeAndVersionAndDefaultVersi
9192
appRegistration.setVersions(versions);
9293
});
9394
}
94-
return new PageImpl<>(resultList, pageable, getTotalCount(cb, predicates.toArray(new Predicate[0])));
95+
return new PageImpl<>(resultList, pageable, getTotalCount(cq));
9596
}
9697

97-
private Long getTotalCount(CriteriaBuilder criteriaBuilder, Predicate[] predicateArray) {
98-
CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
99-
Root<AppRegistration> root = criteriaQuery.from(AppRegistration.class);
100-
101-
criteriaQuery.select(criteriaBuilder.count(root));
102-
criteriaQuery.where(predicateArray);
103-
104-
return entityManager.createQuery(criteriaQuery).getSingleResult();
98+
private Long getTotalCount(CriteriaQuery<AppRegistration> criteriaQuery) {
99+
return (Long) entityManager.createQuery(((SqmSelectStatement)criteriaQuery).createCountQuery()).getSingleResult();
105100
}
106101

107102
}

spring-cloud-skipper/spring-cloud-skipper/pom.xml

-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
<groupId>org.hibernate.orm</groupId>
6060
<artifactId>hibernate-core</artifactId>
6161
<scope>provided</scope>
62-
<version>6.1.7.Final</version>
6362
</dependency>
6463
<dependency>
6564
<groupId>org.zeroturnaround</groupId>

0 commit comments

Comments
 (0)