diff --git a/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java index 05613f689..5baa24887 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java @@ -45,6 +45,7 @@ import com.mysema.query.types.path.PathBuilder; * * @author Oliver Gierke * @author Thomas Darimont + * @author Jocelyn Ntakpe */ public class QueryDslJpaRepository extends SimpleJpaRepository implements QueryDslPredicateExecutor { @@ -135,7 +136,7 @@ public class QueryDslJpaRepository extends SimpleJpa @Override public Page findAll(Predicate predicate, Pageable pageable) { - JPQLQuery countQuery = createQuery(predicate); + JPQLQuery countQuery = createCountQuery(predicate); JPQLQuery query = querydsl.applyPagination(pageable, createQuery(predicate)); Long total = countQuery.count(); @@ -187,6 +188,16 @@ public class QueryDslJpaRepository extends SimpleJpa return query; } + /** + * Creates a new {@link JPQLQuery} count query for the given {@link Predicate}. + * + * @param predicate, can be {@literal null}. + * @return the Querydsl count {@link JPQLQuery}. + */ + protected JPQLQuery createCountQuery(Predicate predicate) { + return querydsl.createQuery(path).where(predicate); + } + /** * Executes the given {@link JPQLQuery} after applying the given {@link OrderSpecifier}s. * diff --git a/src/test/java/org/springframework/data/jpa/repository/EntityGraphRepositoryMethodsIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/EntityGraphRepositoryMethodsIntegrationTests.java index 143990352..c23f16c80 100644 --- a/src/test/java/org/springframework/data/jpa/repository/EntityGraphRepositoryMethodsIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/EntityGraphRepositoryMethodsIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,6 +29,9 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.jpa.domain.sample.QUser; import org.springframework.data.jpa.domain.sample.Role; import org.springframework.data.jpa.domain.sample.User; import org.springframework.data.jpa.repository.sample.RepositoryMethodsWithEntityGraphConfigRepository; @@ -41,6 +44,7 @@ import org.springframework.transaction.annotation.Transactional; * * @author Thomas Darimont * @author Oliver Gierke + * @author Jocelyn Ntakpe */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:config/namespace-autoconfig-context.xml") @@ -128,4 +132,20 @@ public class EntityGraphRepositoryMethodsIntegrationTests { assertThat("colleages should be fetched with 'user.detail' fetchgraph", Persistence.getPersistenceUtil().isLoaded(user.getColleagues()), is(true)); } + + /** + * @see DATAJPA-790 + */ + @Test + public void shouldRespectConfiguredJpaEntityGraphWithPaginationAndQueryDslPredicates() { + + Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em)); + + Page page = repository.findAll(QUser.user.firstname.isNotNull(), new PageRequest(0, 100)); + List result = page.getContent(); + + assertThat(result.size(), is(2)); + assertThat(Persistence.getPersistenceUtil().isLoaded(result.get(0).getRoles()), is(true)); + assertThat(result.get(0), is(tom)); + } } diff --git a/src/test/java/org/springframework/data/jpa/repository/sample/RepositoryMethodsWithEntityGraphConfigRepository.java b/src/test/java/org/springframework/data/jpa/repository/sample/RepositoryMethodsWithEntityGraphConfigRepository.java index a8b264076..9ddb82755 100644 --- a/src/test/java/org/springframework/data/jpa/repository/sample/RepositoryMethodsWithEntityGraphConfigRepository.java +++ b/src/test/java/org/springframework/data/jpa/repository/sample/RepositoryMethodsWithEntityGraphConfigRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2015 the original author or authors. + * Copyright 2014-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,19 +17,25 @@ package org.springframework.data.jpa.repository.sample; import java.util.List; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.domain.sample.User; import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.EntityGraph.EntityGraphType; -import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.querydsl.QueryDslPredicateExecutor; import org.springframework.data.repository.CrudRepository; +import com.mysema.query.types.Predicate; + /** * Custom repository interface that customizes the fetching behavior of querys of well known repository interface * methods via {@link EntityGraph} annotation. * * @author Thomas Darimont + * @author Jocelyn Ntakpe */ -public interface RepositoryMethodsWithEntityGraphConfigRepository extends CrudRepository { +public interface RepositoryMethodsWithEntityGraphConfigRepository + extends CrudRepository, QueryDslPredicateExecutor { /** * Should find all users. @@ -48,10 +54,16 @@ public interface RepositoryMethodsWithEntityGraphConfigRepository extends CrudRe */ @EntityGraph User getOneWithDefinedEntityGraphById(Integer id); - + /** * @see DATAJPA-696 */ @EntityGraph(attributePaths = { "roles", "colleagues.roles" }) User getOneWithAttributeNamesById(Integer id); + + /** + * @see DATAJPA-790 + */ + @EntityGraph("User.detail") + Page findAll(Predicate predicate, Pageable pageable); }