Skip to content

Commit dd0919b

Browse files
jntakpeodrotbohm
authored andcommitted
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.
1 parent 8220de2 commit dd0919b

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
*
4646
* @author Oliver Gierke
4747
* @author Thomas Darimont
48+
* @author Jocelyn Ntakpe
4849
*/
4950
public class QueryDslJpaRepository<T, ID extends Serializable> extends SimpleJpaRepository<T, ID>
5051
implements QueryDslPredicateExecutor<T> {
@@ -136,7 +137,7 @@ public List<T> findAll(OrderSpecifier<?>... orders) {
136137
@Override
137138
public Page<T> findAll(Predicate predicate, Pageable pageable) {
138139

139-
JPQLQuery<?> countQuery = createQuery(predicate);
140+
JPQLQuery<?> countQuery = createCountQuery(predicate);
140141
JPQLQuery<T> query = querydsl.applyPagination(pageable, createQuery(predicate).select(path));
141142

142143
long total = countQuery.fetchCount();
@@ -188,6 +189,16 @@ protected JPQLQuery<?> createQuery(Predicate... predicate) {
188189
return query;
189190
}
190191

192+
/**
193+
* Creates a new {@link JPQLQuery} count query for the given {@link Predicate}.
194+
*
195+
* @param predicate
196+
* @return the Querydsl count {@link JPQLQuery}.
197+
*/
198+
protected JPQLQuery<?> createCountQuery(Predicate predicate) {
199+
return querydsl.createQuery(path).where(predicate);
200+
}
201+
191202
/**
192203
* Executes the given {@link JPQLQuery} after applying the given {@link OrderSpecifier}s.
193204
*

src/test/java/org/springframework/data/jpa/repository/EntityGraphRepositoryMethodsIntegrationTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
import org.junit.Test;
3030
import org.junit.runner.RunWith;
3131
import org.springframework.beans.factory.annotation.Autowired;
32+
import org.springframework.data.domain.Page;
33+
import org.springframework.data.domain.PageRequest;
34+
import org.springframework.data.jpa.domain.sample.QUser;
3235
import org.springframework.data.jpa.domain.sample.Role;
3336
import org.springframework.data.jpa.domain.sample.User;
3437
import org.springframework.data.jpa.repository.sample.RepositoryMethodsWithEntityGraphConfigRepository;
@@ -128,4 +131,18 @@ public void shouldRespectDynamicFetchGraphForGetOneWithAttributeNamesById() {
128131
assertThat("colleages should be fetched with 'user.detail' fetchgraph",
129132
Persistence.getPersistenceUtil().isLoaded(user.getColleagues()), is(true));
130133
}
134+
135+
/**
136+
* @see DATAJPA-790
137+
*/
138+
@Test
139+
public void shouldRespectConfiguredJpaEntityGraphWithPaginationAndQueryDslPredicates() {
140+
Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
141+
Page<User> page = repository.findAll(QUser.user.firstname.isNotNull(), new PageRequest(0, 100));
142+
List<User> result = page.getContent();
143+
assertThat(result.size(), is(2));
144+
assertThat(Persistence.getPersistenceUtil().isLoaded(result.get(0).getRoles()), is(true));
145+
assertThat(result.get(0), is(tom));
146+
}
147+
131148
}

src/test/java/org/springframework/data/jpa/repository/sample/RepositoryMethodsWithEntityGraphConfigRepository.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@
1717

1818
import java.util.List;
1919

20+
import com.querydsl.core.types.Predicate;
21+
import org.springframework.data.domain.Page;
22+
import org.springframework.data.domain.Pageable;
2023
import org.springframework.data.jpa.domain.sample.User;
2124
import org.springframework.data.jpa.repository.EntityGraph;
2225
import org.springframework.data.jpa.repository.EntityGraph.EntityGraphType;
23-
import org.springframework.data.jpa.repository.JpaRepository;
26+
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
2427
import org.springframework.data.repository.CrudRepository;
2528

2629
/**
2730
* Custom repository interface that customizes the fetching behavior of querys of well known repository interface
2831
* methods via {@link EntityGraph} annotation.
2932
*
3033
* @author Thomas Darimont
34+
* @author Jocelyn Ntakpe
3135
*/
32-
public interface RepositoryMethodsWithEntityGraphConfigRepository extends CrudRepository<User, Integer> {
36+
public interface RepositoryMethodsWithEntityGraphConfigRepository extends CrudRepository<User, Integer>, QueryDslPredicateExecutor<User> {
3337

3438
/**
3539
* Should find all users.
@@ -54,4 +58,10 @@ public interface RepositoryMethodsWithEntityGraphConfigRepository extends CrudRe
5458
*/
5559
@EntityGraph(attributePaths = { "roles", "colleagues.roles" })
5660
User getOneWithAttributeNamesById(Integer id);
61+
62+
/**
63+
* @see DATAJPA-790
64+
*/
65+
@EntityGraph(type = EntityGraphType.FETCH, value = "User.detail")
66+
Page<User> findAll(Predicate predicate, Pageable pageable);
5767
}

0 commit comments

Comments
 (0)