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
42224b2204
commit
dc230ef267
@@ -45,6 +45,7 @@ import com.mysema.query.types.path.PathBuilder;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
* @author Jocelyn Ntakpe
|
||||
*/
|
||||
public class QueryDslJpaRepository<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements
|
||||
QueryDslPredicateExecutor<T> {
|
||||
@@ -135,7 +136,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 query = querydsl.applyPagination(pageable, createQuery(predicate));
|
||||
|
||||
Long total = countQuery.count();
|
||||
@@ -187,6 +188,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, can be {@literal null}.
|
||||
* @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.
|
||||
*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2015 the original author or authors.
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -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;
|
||||
@@ -41,6 +44,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @author Oliver Gierke
|
||||
* @author Jocelyn Ntakpe
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:config/namespace-autoconfig-context.xml")
|
||||
@@ -128,4 +132,20 @@ 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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2015 the original author or authors.
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,19 +17,25 @@ package org.springframework.data.jpa.repository.sample;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
|
||||
import com.mysema.query.types.Predicate;
|
||||
|
||||
/**
|
||||
* Custom repository interface that customizes the fetching behavior of querys of well known repository interface
|
||||
* 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.
|
||||
@@ -48,10 +54,16 @@ public interface RepositoryMethodsWithEntityGraphConfigRepository extends CrudRe
|
||||
*/
|
||||
@EntityGraph
|
||||
User getOneWithDefinedEntityGraphById(Integer id);
|
||||
|
||||
|
||||
/**
|
||||
* @see DATAJPA-696
|
||||
*/
|
||||
@EntityGraph(attributePaths = { "roles", "colleagues.roles" })
|
||||
User getOneWithAttributeNamesById(Integer id);
|
||||
|
||||
/**
|
||||
* @see DATAJPA-790
|
||||
*/
|
||||
@EntityGraph("User.detail")
|
||||
Page<User> findAll(Predicate predicate, Pageable pageable);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user