Browse files

Fixed bug in total page number calculation due to additional root add…

…ing.

Using the initially created root to create the count projection now. Added more tests to verify correct number of elements and pages.
  • Loading branch information...
1 parent e5b31d6 commit cd4013fe1d845cab1ddd4859e8413b09ad589700 @olivergierke olivergierke committed Feb 14, 2011
View
14 src/main/java/org/springframework/data/jpa/repository/query/JpaCountQueryCreator.java
@@ -19,6 +19,7 @@
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
+import javax.persistence.criteria.Root;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.SimpleParameterAccessor;
@@ -32,9 +33,6 @@
*/
public class JpaCountQueryCreator extends JpaQueryCreator {
- private final Class<?> domainClass;
-
-
/**
* Creates a new {@link JpaCountQueryCreator}.
*
@@ -48,24 +46,24 @@ public JpaCountQueryCreator(PartTree tree,
EntityManager em) {
super(tree, parameters, domainClass, em);
- this.domainClass = domainClass;
}
/*
* (non-Javadoc)
*
* @see
- * org.springframework.data.jpa.repository.query.JpaQueryCreator#finalize
+ * org.springframework.data.jpa.repository.query.JpaQueryCreator#complete
* (javax.persistence.criteria.Predicate,
* org.springframework.data.domain.Sort,
* javax.persistence.criteria.CriteriaQuery,
- * javax.persistence.criteria.CriteriaBuilder)
+ * javax.persistence.criteria.CriteriaBuilder,
+ * javax.persistence.criteria.Root)
*/
@Override
protected CriteriaQuery<Object> complete(Predicate predicate, Sort sort,
- CriteriaQuery<Object> query, CriteriaBuilder builder) {
+ CriteriaQuery<Object> query, CriteriaBuilder builder, Root<?> root) {
- return query.select(builder.count(query.from(domainClass)));
+ return query.select(builder.count(root));
}
}
View
4 src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java
@@ -125,7 +125,7 @@ protected Predicate or(Predicate base, Predicate predicate) {
protected final CriteriaQuery<Object> complete(Predicate predicate,
Sort sort) {
- return complete(predicate, sort, query, builder);
+ return complete(predicate, sort, query, builder, root);
}
@@ -140,7 +140,7 @@ protected Predicate or(Predicate base, Predicate predicate) {
* @return
*/
protected CriteriaQuery<Object> complete(Predicate predicate, Sort sort,
- CriteriaQuery<Object> query, CriteriaBuilder builder) {
+ CriteriaQuery<Object> query, CriteriaBuilder builder, Root<?> root) {
return this.query.select(root).where(predicate)
.orderBy(QueryUtils.toOrders(sort, root, builder));
View
4 src/main/java/org/springframework/data/jpa/repository/query/PartTreeJpaQuery.java
@@ -89,11 +89,11 @@ public Query createQuery(Object[] parameters) {
*/
public Query createCountQuery(Object[] parameters) {
- CriteriaQuery<Object> createQuery =
+ CriteriaQuery<Object> query =
new JpaCountQueryCreator(tree, new SimpleParameterAccessor(
getParameters(), parameters), domainClass,
getEntityManager()).createQuery();
- return getEntityManager().createQuery(createQuery);
+ return getEntityManager().createQuery(query);
}
View
4 src/test/java/org/springframework/data/jpa/repository/UserRepositoryFinderTests.java
@@ -119,7 +119,9 @@ public void executesPagingMethodToPageCorrectly() throws Exception {
Page<User> page =
userRepository.findByFirstname(new PageRequest(0, 1), "foobar");
- assertEquals(1, page.getNumberOfElements());
+ assertThat(page.getNumberOfElements(), is(1));
+ assertThat(page.getTotalElements(), is(2L));
+ assertThat(page.getTotalPages(), is(2));
}

0 comments on commit cd4013f

Please sign in to comment.