DATAJPA-656 - Make sure SimpleJpaRepository.count(Specification) works with group clauses.

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.
This commit is contained in:
Oliver Gierke
2015-02-10 18:50:31 +01:00
parent 49f9df6602
commit 9522885660
2 changed files with 22 additions and 23 deletions

View File

@@ -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<Long> query) {
Assert.notNull(query);
List<Long> 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}.
*

View File

@@ -394,7 +394,7 @@ public class SimpleJpaRepository<T, ID extends Serializable> implements JpaRepos
*/
public long count(Specification<T> spec) {
return getCountQuery(spec).getSingleResult();
return executeCountQuery(getCountQuery(spec));
}
/*
@@ -469,7 +469,7 @@ public class SimpleJpaRepository<T, ID extends Serializable> implements JpaRepos
query.setFirstResult(pageable.getOffset());
query.setMaxResults(pageable.getPageSize());
Long total = QueryUtils.executeCountQuery(getCountQuery(spec));
Long total = executeCountQuery(getCountQuery(spec));
List<T> content = total > pageable.getOffset() ? query.getResultList() : Collections.<T> emptyList();
return new PageImpl<T>(content, pageable, total);
@@ -574,6 +574,26 @@ public class SimpleJpaRepository<T, ID extends Serializable> 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<Long> query) {
Assert.notNull(query);
List<Long> 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