DATAJPA-790 - QueryException when applying @EntityGraph on findAll(Predicate,Pageable).
We now create the count query that's required for pagination queries without applying the query hints. Related tickets: DATAJPA-684 Original pull request: #182.
This commit is contained in:
committed by
Oliver Gierke
parent
8220de248c
commit
fea2f762ca
@@ -45,6 +45,7 @@ import com.querydsl.jpa.impl.AbstractJPAQuery;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
* @author Jocelyn Ntakpe
|
||||
*/
|
||||
public class QueryDslJpaRepository<T, ID extends Serializable> extends SimpleJpaRepository<T, ID>
|
||||
implements QueryDslPredicateExecutor<T> {
|
||||
@@ -136,7 +137,7 @@ public class QueryDslJpaRepository<T, ID extends Serializable> extends SimpleJpa
|
||||
@Override
|
||||
public Page<T> findAll(Predicate predicate, Pageable pageable) {
|
||||
|
||||
JPQLQuery<?> countQuery = createQuery(predicate);
|
||||
JPQLQuery<?> countQuery = createCountQuery(predicate);
|
||||
JPQLQuery<T> query = querydsl.applyPagination(pageable, createQuery(predicate).select(path));
|
||||
|
||||
long total = countQuery.fetchCount();
|
||||
@@ -188,6 +189,16 @@ public class QueryDslJpaRepository<T, ID extends Serializable> extends SimpleJpa
|
||||
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.
|
||||
*
|
||||
|
||||
@@ -29,6 +29,9 @@ import org.junit.Before;
|
||||
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 class EntityGraphRepositoryMethodsIntegrationTests {
|
||||
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<User> page = repository.findAll(QUser.user.firstname.isNotNull(), new PageRequest(0, 100));
|
||||
List<User> result = page.getContent();
|
||||
assertThat(result.size(), is(2));
|
||||
assertThat(Persistence.getPersistenceUtil().isLoaded(result.get(0).getRoles()), is(true));
|
||||
assertThat(result.get(0), is(tom));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,10 +17,13 @@ package org.springframework.data.jpa.repository.sample;
|
||||
|
||||
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 @@ import org.springframework.data.repository.CrudRepository;
|
||||
* methods via {@link EntityGraph} annotation.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @author Jocelyn Ntakpe
|
||||
*/
|
||||
public interface RepositoryMethodsWithEntityGraphConfigRepository extends CrudRepository<User, Integer> {
|
||||
public interface RepositoryMethodsWithEntityGraphConfigRepository extends CrudRepository<User, Integer>, QueryDslPredicateExecutor<User> {
|
||||
|
||||
/**
|
||||
* 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<User> findAll(Predicate predicate, Pageable pageable);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user