Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

DATAJPA-257 - Make count query execution in SimpleJpaRepository more …

…robust.

The execution of a count query can potentially return multiple values instead of just a single one. This causes persistence providers to throw an exception as we trigger ….getSingleResult() in SimpleJpaRepository. We're now calling …getResultList() and sum up all values returned.
  • Loading branch information...
commit e28b6fc5226b209ee5e13629c658186c943d35a3 1 parent 3b2818d
@olivergierke olivergierke authored
View
3  src/main/java/org/springframework/data/jpa/repository/query/JpaQueryExecution.java
@@ -97,6 +97,7 @@ protected Object doExecute(AbstractJpaQuery repositoryQuery, Object[] values) {
// Execute query to compute total
TypedQuery<Long> projection = repositoryQuery.createCountQuery(values);
+
List<Long> totals = projection.getResultList();
Long total = totals.size() == 1 ? totals.get(0) : totals.size();
@@ -160,4 +161,4 @@ protected Object doExecute(AbstractJpaQuery query, Object[] values) {
return result;
}
}
-}
+}
View
21 src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java
@@ -29,6 +29,7 @@
import javax.persistence.EntityManager;
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;
@@ -329,6 +330,26 @@ public static boolean hasNamedParameter(Query query) {
}
/**
+ * 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}.
*
* @param order the order to transform into a JPA {@link javax.persistence.criteria.Order}
View
3  src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java
@@ -40,6 +40,7 @@
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.query.QueryUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
@@ -402,7 +403,7 @@ public void flush() {
query.setFirstResult(pageable.getOffset());
query.setMaxResults(pageable.getPageSize());
- Long total = getCountQuery(spec).getSingleResult();
+ Long total = QueryUtils.executeCountQuery(getCountQuery(spec));
List<T> content = total > pageable.getOffset() ? query.getResultList() : Collections.<T> emptyList();
return new PageImpl<T>(content, pageable, total);
Please sign in to comment.
Something went wrong with that request. Please try again.