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 950caa32e3..401465df4d 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 @@ -46,6 +46,7 @@ * @author Oliver Gierke * @author Thomas Darimont * @author Mark Paluch + * @author Jocelyn Ntakpe */ public class QueryDslJpaRepository extends SimpleJpaRepository implements QueryDslPredicateExecutor { @@ -137,7 +138,7 @@ public List findAll(OrderSpecifier... orders) { @Override public Page findAll(Predicate predicate, Pageable pageable) { - final JPQLQuery countQuery = createQuery(predicate); + final JPQLQuery countQuery = createCountQuery(predicate); JPQLQuery query = querydsl.applyPagination(pageable, createQuery(predicate).select(path)); return PageableExecutionUtils.getPage(query.fetch(), pageable, new TotalSupplier() { @@ -192,6 +193,16 @@ protected JPQLQuery createQuery(Predicate... predicate) { return query; } + /** + * Creates a new {@link JPQLQuery} count query for the given {@link Predicate}. + * + * @param predicate + * @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 143990352c..03bccfb6c4 100644 --- a/src/test/java/org/springframework/data/jpa/repository/EntityGraphRepositoryMethodsIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/EntityGraphRepositoryMethodsIntegrationTests.java @@ -29,6 +29,9 @@ 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; @@ -128,4 +131,18 @@ public void shouldRespectDynamicFetchGraphForGetOneWithAttributeNamesById() { 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 a8b264076d..23f2350ece 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 @@ -17,10 +17,13 @@ import java.util.List; +import com.querydsl.core.types.Predicate; +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; /** @@ -28,8 +31,9 @@ * 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. @@ -54,4 +58,10 @@ public interface RepositoryMethodsWithEntityGraphConfigRepository extends CrudRe */ @EntityGraph(attributePaths = { "roles", "colleagues.roles" }) User getOneWithAttributeNamesById(Integer id); + + /** + * @see DATAJPA-790 + */ + @EntityGraph(type = EntityGraphType.FETCH, value = "User.detail") + Page findAll(Predicate predicate, Pageable pageable); }