From f0bf54c01bff81613e7a42fb5eafb2d2f0f39e48 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Thu, 19 Oct 2017 14:09:32 +0200 Subject: [PATCH] DATAJPA-1207 - Apply fetch graph to queries from specifications. This just adds a test since the fetch graph already does get applied. Original Pull Request: #229 --- ...raphRepositoryMethodsIntegrationTests.java | 31 +++++++++++++++++++ ...ethodsWithEntityGraphConfigRepository.java | 11 ++++++- 2 files changed, 41 insertions(+), 1 deletion(-) 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 bdd482f13..4c6bacb3d 100644 --- a/src/test/java/org/springframework/data/jpa/repository/EntityGraphRepositoryMethodsIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/EntityGraphRepositoryMethodsIntegrationTests.java @@ -24,6 +24,10 @@ import java.util.List; import javax.persistence.EntityManager; import javax.persistence.Persistence; import javax.persistence.PersistenceUtil; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; import org.junit.Assume; import org.junit.Before; @@ -32,9 +36,11 @@ 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.Specification; 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.domain.sample.User_; import org.springframework.data.jpa.repository.sample.RepositoryMethodsWithEntityGraphConfigRepository; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -47,6 +53,7 @@ import org.springframework.transaction.annotation.Transactional; * @author Oliver Gierke * @author Jocelyn Ntakpe * @author Christoph Strobl + * @author Jens Schauder */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:config/namespace-autoconfig-context.xml") @@ -162,6 +169,25 @@ public class EntityGraphRepositoryMethodsIntegrationTests { assertThat(result.get(0), is(tom)); } + @Test // DATAJPA-1207 + public void shouldRespectConfiguredJpaEntityGraphWithPaginationAndSpecification() { + + Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em)); + em.flush(); + em.clear(); + + Page page = repository.findAll( // + (Specification) this::firstNameIsNotNull, // + PageRequest.of(0, 100) // + ); + + List result = page.getContent(); + + assertThat(result.size(), is(3)); + assertThat(util.isLoaded(result.get(0).getRoles()), is(true)); + assertThat(result.get(0), is(tom)); + } + @Test // DATAJPA-1041 public void shouldRespectNamedEntitySubGraph() { @@ -229,4 +255,9 @@ public class EntityGraphRepositoryMethodsIntegrationTests { } } } + + private Predicate firstNameIsNotNull(Root root, CriteriaQuery __, CriteriaBuilder criteriaBuilder) { + return criteriaBuilder.isNotNull(root.get(User_.firstname)); + } + } 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 a55555588..5592f719e 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 @@ -20,11 +20,14 @@ import java.util.Optional; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.domain.Specification; 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.JpaSpecificationExecutor; import org.springframework.data.querydsl.QuerydslPredicateExecutor; import org.springframework.data.repository.CrudRepository; +import org.springframework.lang.Nullable; import com.querydsl.core.types.Predicate; @@ -35,9 +38,10 @@ import com.querydsl.core.types.Predicate; * @author Thomas Darimont * @author Jocelyn Ntakpe * @author Christoph Strobl + * @author Jens Schauder */ public interface RepositoryMethodsWithEntityGraphConfigRepository - extends CrudRepository, QuerydslPredicateExecutor { + extends CrudRepository, QuerydslPredicateExecutor, JpaSpecificationExecutor { /** * Should find all users. @@ -63,6 +67,11 @@ public interface RepositoryMethodsWithEntityGraphConfigRepository @EntityGraph("User.detail") Page findAll(Predicate predicate, Pageable pageable); + // DATAJPA-1207 + @Override + @EntityGraph("User.detail") + Page findAll(@Nullable Specification spec, Pageable pageable); + // DATAJPA-1041 @EntityGraph(type = EntityGraphType.FETCH, value = "User.withSubGraph") User findOneWithMultipleSubGraphsUsingNamedEntityGraphById(Integer id);