Skip to content

Commit dc230ef

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 42224b2 commit dc230ef

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
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> implements
5051
QueryDslPredicateExecutor<T> {
@@ -135,7 +136,7 @@ public List<T> findAll(OrderSpecifier<?>... orders) {
135136
@Override
136137
public Page<T> findAll(Predicate predicate, Pageable pageable) {
137138

138-
JPQLQuery countQuery = createQuery(predicate);
139+
JPQLQuery countQuery = createCountQuery(predicate);
139140
JPQLQuery query = querydsl.applyPagination(pageable, createQuery(predicate));
140141

141142
Long total = countQuery.count();
@@ -187,6 +188,16 @@ protected JPQLQuery createQuery(Predicate... predicate) {
187188
return query;
188189
}
189190

191+
/**
192+
* Creates a new {@link JPQLQuery} count query for the given {@link Predicate}.
193+
*
194+
* @param predicate, can be {@literal null}.
195+
* @return the Querydsl count {@link JPQLQuery}.
196+
*/
197+
protected JPQLQuery createCountQuery(Predicate predicate) {
198+
return querydsl.createQuery(path).where(predicate);
199+
}
200+
190201
/**
191202
* Executes the given {@link JPQLQuery} after applying the given {@link OrderSpecifier}s.
192203
*

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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;
@@ -41,6 +44,7 @@
4144
*
4245
* @author Thomas Darimont
4346
* @author Oliver Gierke
47+
* @author Jocelyn Ntakpe
4448
*/
4549
@RunWith(SpringJUnit4ClassRunner.class)
4650
@ContextConfiguration("classpath:config/namespace-autoconfig-context.xml")
@@ -128,4 +132,20 @@ public void shouldRespectDynamicFetchGraphForGetOneWithAttributeNamesById() {
128132
assertThat("colleages should be fetched with 'user.detail' fetchgraph",
129133
Persistence.getPersistenceUtil().isLoaded(user.getColleagues()), is(true));
130134
}
135+
136+
/**
137+
* @see DATAJPA-790
138+
*/
139+
@Test
140+
public void shouldRespectConfiguredJpaEntityGraphWithPaginationAndQueryDslPredicates() {
141+
142+
Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
143+
144+
Page<User> page = repository.findAll(QUser.user.firstname.isNotNull(), new PageRequest(0, 100));
145+
List<User> result = page.getContent();
146+
147+
assertThat(result.size(), is(2));
148+
assertThat(Persistence.getPersistenceUtil().isLoaded(result.get(0).getRoles()), is(true));
149+
assertThat(result.get(0), is(tom));
150+
}
131151
}

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2015 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,19 +17,25 @@
1717

1818
import java.util.List;
1919

20+
import org.springframework.data.domain.Page;
21+
import org.springframework.data.domain.Pageable;
2022
import org.springframework.data.jpa.domain.sample.User;
2123
import org.springframework.data.jpa.repository.EntityGraph;
2224
import org.springframework.data.jpa.repository.EntityGraph.EntityGraphType;
23-
import org.springframework.data.jpa.repository.JpaRepository;
25+
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
2426
import org.springframework.data.repository.CrudRepository;
2527

28+
import com.mysema.query.types.Predicate;
29+
2630
/**
2731
* Custom repository interface that customizes the fetching behavior of querys of well known repository interface
2832
* methods via {@link EntityGraph} annotation.
2933
*
3034
* @author Thomas Darimont
35+
* @author Jocelyn Ntakpe
3136
*/
32-
public interface RepositoryMethodsWithEntityGraphConfigRepository extends CrudRepository<User, Integer> {
37+
public interface RepositoryMethodsWithEntityGraphConfigRepository
38+
extends CrudRepository<User, Integer>, QueryDslPredicateExecutor<User> {
3339

3440
/**
3541
* Should find all users.
@@ -48,10 +54,16 @@ public interface RepositoryMethodsWithEntityGraphConfigRepository extends CrudRe
4854
*/
4955
@EntityGraph
5056
User getOneWithDefinedEntityGraphById(Integer id);
51-
57+
5258
/**
5359
* @see DATAJPA-696
5460
*/
5561
@EntityGraph(attributePaths = { "roles", "colleagues.roles" })
5662
User getOneWithAttributeNamesById(Integer id);
63+
64+
/**
65+
* @see DATAJPA-790
66+
*/
67+
@EntityGraph("User.detail")
68+
Page<User> findAll(Predicate predicate, Pageable pageable);
5769
}

0 commit comments

Comments
 (0)