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:
Jocelyn Ntakpe
2016-09-28 14:55:35 +02:00
committed by Oliver Gierke
parent b2426d89ed
commit 3f5467dc85
3 changed files with 41 additions and 3 deletions

View File

@@ -46,6 +46,7 @@ import com.querydsl.jpa.impl.AbstractJPAQuery;
* @author Oliver Gierke
* @author Thomas Darimont
* @author Mark Paluch
* @author Jocelyn Ntakpe
*/
public class QueryDslJpaRepository<T, ID extends Serializable> extends SimpleJpaRepository<T, ID>
implements QueryDslPredicateExecutor<T> {
@@ -137,7 +138,7 @@ public class QueryDslJpaRepository<T, ID extends Serializable> extends SimpleJpa
@Override
public Page<T> findAll(Predicate predicate, Pageable pageable) {
final JPQLQuery<?> countQuery = createQuery(predicate);
final JPQLQuery<?> countQuery = createCountQuery(predicate);
JPQLQuery<T> query = querydsl.applyPagination(pageable, createQuery(predicate).select(path));
return PageableExecutionUtils.getPage(query.fetch(), pageable, new TotalSupplier() {
@@ -192,6 +193,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.
*

View File

@@ -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));
}
}

View File

@@ -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);
}