Skip to content

Commit

Permalink
Reuse CriteriaQuery when retrieving result count. (spring-cloud#5702)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cppwfs authored Feb 22, 2024
1 parent 32fc5b7 commit bed42bf
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 18 deletions.
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

0 comments on commit bed42bf

Please sign in to comment.