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
This commit is contained in:
Jens Schauder
2017-10-19 14:09:32 +02:00
committed by Christoph Strobl
parent dcfc9838d4
commit f0bf54c01b
2 changed files with 41 additions and 1 deletions

View File

@@ -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<User> page = repository.findAll( //
(Specification<User>) this::firstNameIsNotNull, //
PageRequest.of(0, 100) //
);
List<User> 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<User> root, CriteriaQuery<?> __, CriteriaBuilder criteriaBuilder) {
return criteriaBuilder.isNotNull(root.get(User_.firstname));
}
}

View File

@@ -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<User, Integer>, QuerydslPredicateExecutor<User> {
extends CrudRepository<User, Integer>, QuerydslPredicateExecutor<User>, JpaSpecificationExecutor {
/**
* Should find all users.
@@ -63,6 +67,11 @@ public interface RepositoryMethodsWithEntityGraphConfigRepository
@EntityGraph("User.detail")
Page<User> findAll(Predicate predicate, Pageable pageable);
// DATAJPA-1207
@Override
@EntityGraph("User.detail")
Page<User> findAll(@Nullable Specification spec, Pageable pageable);
// DATAJPA-1041
@EntityGraph(type = EntityGraphType.FETCH, value = "User.withSubGraph")
User findOneWithMultipleSubGraphsUsingNamedEntityGraphById(Integer id);