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:
committed by
Christoph Strobl
parent
96ccca2987
commit
21f658d702
@@ -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<User> 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<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 +258,9 @@ public class EntityGraphRepositoryMethodsIntegrationTests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Predicate firstNameIsNotNull(Root<User> root, CriteriaQuery<?> __, CriteriaBuilder criteriaBuilder) {
|
||||
return criteriaBuilder.isNotNull(root.get(User_.firstname));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<User, Integer>, QueryDslPredicateExecutor<User> {
|
||||
extends CrudRepository<User, Integer>, QueryDslPredicateExecutor<User>, JpaSpecificationExecutor {
|
||||
|
||||
/**
|
||||
* Should find all users.
|
||||
@@ -62,6 +65,11 @@ public interface RepositoryMethodsWithEntityGraphConfigRepository
|
||||
@EntityGraph("User.detail")
|
||||
Page<User> findAll(Predicate predicate, Pageable pageable);
|
||||
|
||||
// DATAJPA-1207
|
||||
@Override
|
||||
@EntityGraph("User.detail")
|
||||
Page<User> findAll(Specification spec, Pageable pageable);
|
||||
|
||||
// DATAJPA-1041
|
||||
@EntityGraph(type = EntityGraphType.FETCH, value = "User.withSubGraph")
|
||||
User findOneWithMultipleSubGraphsUsingNamedEntityGraphById(Integer id);
|
||||
|
||||
Reference in New Issue
Block a user