From 9522885660ef3e1696f9ca0b9105855621bd7cf7 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Tue, 10 Feb 2015 18:50:31 +0100 Subject: [PATCH] DATAJPA-656 - Make sure SimpleJpaRepository.count(Specification) works with group clauses. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now use the custom execution of count queries previously located in QueryUtils.executeCountQuery(…) from within SimpleJpaRepository.count(Specification). Moved the static helper into SimpleJpaRepository ti minimized exposed API. --- .../data/jpa/repository/query/QueryUtils.java | 21 ---------------- .../support/SimpleJpaRepository.java | 24 +++++++++++++++++-- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java b/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java index f48d088f0..a4d2371de 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java @@ -38,7 +38,6 @@ import javax.persistence.ManyToOne; import javax.persistence.OneToOne; import javax.persistence.Parameter; import javax.persistence.Query; -import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.Expression; import javax.persistence.criteria.From; @@ -417,26 +416,6 @@ public abstract class QueryUtils { return orders; } - /** - * Executes a count query and transparently sums up all values returned. - * - * @param query must not be {@literal null}. - * @return - */ - public static Long executeCountQuery(TypedQuery query) { - - Assert.notNull(query); - - List totals = query.getResultList(); - Long total = 0L; - - for (Long element : totals) { - total += element == null ? 0 : element; - } - - return total; - } - /** * Creates a criteria API {@link javax.persistence.criteria.Order} from the given {@link Order}. * diff --git a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index eb9060e5b..c4d919c36 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -394,7 +394,7 @@ public class SimpleJpaRepository implements JpaRepos */ public long count(Specification spec) { - return getCountQuery(spec).getSingleResult(); + return executeCountQuery(getCountQuery(spec)); } /* @@ -469,7 +469,7 @@ public class SimpleJpaRepository implements JpaRepos query.setFirstResult(pageable.getOffset()); query.setMaxResults(pageable.getPageSize()); - Long total = QueryUtils.executeCountQuery(getCountQuery(spec)); + Long total = executeCountQuery(getCountQuery(spec)); List content = total > pageable.getOffset() ? query.getResultList() : Collections. emptyList(); return new PageImpl(content, pageable, total); @@ -574,6 +574,26 @@ public class SimpleJpaRepository implements JpaRepos return Jpa21Utils.tryConfigureFetchGraph(em, toReturn, metadata.getEntityGraph()); } + /** + * Executes a count query and transparently sums up all values returned. + * + * @param query must not be {@literal null}. + * @return + */ + private static Long executeCountQuery(TypedQuery query) { + + Assert.notNull(query); + + List totals = query.getResultList(); + Long total = 0L; + + for (Long element : totals) { + total += element == null ? 0 : element; + } + + return total; + } + /** * Specification that gives access to the {@link Parameter} instance used to bind the ids for * {@link SimpleJpaRepository#findAll(Iterable)}. Workaround for OpenJPA not binding collections to in-clauses