Skip to content

DATAJPA-790 - QueryException when applying @EntityGraph on findAll with QueryDSL #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* @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> {
Expand Down Expand Up @@ -137,7 +138,7 @@ public List<T> findAll(OrderSpecifier<?>... orders) {
@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() {
Expand Down Expand Up @@ -192,6 +193,16 @@ protected JPQLQuery<?> createQuery(Predicate... predicate) {
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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
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;
Expand Down Expand Up @@ -128,4 +131,18 @@ public void shouldRespectDynamicFetchGraphForGetOneWithAttributeNamesById() {
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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@

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;

/**
* 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.
Expand All @@ -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);
}