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 5465ebb30..8f8ac8359 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,28 @@ 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(new Specification() { + @Override + public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { + return cb.isNotNull(root.get(User_.firstname)); + } + }, new PageRequest(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 +258,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 9beb2e80c..c5d54828f 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 @@ -19,9 +19,11 @@ import java.util.List; 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; @@ -34,9 +36,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. @@ -62,6 +65,11 @@ public interface RepositoryMethodsWithEntityGraphConfigRepository @EntityGraph("User.detail") Page findAll(Predicate predicate, Pageable pageable); + // DATAJPA-1207 + @Override + @EntityGraph("User.detail") + Page findAll(Specification spec, Pageable pageable); + // DATAJPA-1041 @EntityGraph(type = EntityGraphType.FETCH, value = "User.withSubGraph") User findOneWithMultipleSubGraphsUsingNamedEntityGraphById(Integer id);