DATAJPA-679 - Added QueryDslPredicateExecutor.findAll(Predicate, Sort).

We now support findAll on QueryDslJpaRepository that accepts a Querydsl Predicate and a Sort and returns a List<T>.

Original pull request: #135.
This commit is contained in:
Thomas Darimont
2015-02-23 11:52:30 +01:00
committed by Oliver Gierke
parent 9522885660
commit 7ed684b0ab
2 changed files with 36 additions and 1 deletions

View File

@@ -27,6 +27,7 @@ import javax.persistence.LockModeType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.query.Jpa21Utils;
import org.springframework.data.jpa.repository.query.JpaEntityGraph;
import org.springframework.data.querydsl.EntityPathResolver;
@@ -114,6 +115,15 @@ public class QueryDslJpaRepository<T, ID extends Serializable> extends SimpleJpa
return executeSorted(createQuery(predicate), orders);
}
/*
* (non-Javadoc)
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.Predicate, org.springframework.data.domain.Sort)
*/
@Override
public List<T> findAll(Predicate predicate, Sort sort) {
return executeSorted(createQuery(predicate), sort);
}
/*
* (non-Javadoc)
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.OrderSpecifier[])
@@ -204,6 +214,17 @@ public class QueryDslJpaRepository<T, ID extends Serializable> extends SimpleJpa
* @return
*/
private List<T> executeSorted(JPQLQuery query, OrderSpecifier<?>... orders) {
return querydsl.applySorting(new QSort(orders), query).list(path);
return executeSorted(query, new QSort(orders));
}
/**
* Executes the given {@link JPQLQuery} after applying the given {@link Sort}.
*
* @param query must not be {@literal null}.
* @param sort must not be {@literal null}.
* @return
*/
private List<T> executeSorted(JPQLQuery query, Sort sort) {
return querydsl.applySorting(sort, query).list(path);
}
}

View File

@@ -336,4 +336,18 @@ public class QueryDslJpaRepositoryTests {
assertThat(repository.exists(user.firstname.eq("Unknown")), is(false));
assertThat(repository.exists((Predicate) null), is(true));
}
/**
* @see DATAJPA-679
*/
@Test
public void shouldSupportFindAllWithPredicateAndSort() {
List<User> users = repository.findAll(user.dateOfBirth.isNull(), new Sort(Direction.ASC, "firstname"));
assertThat(users, hasSize(3));
assertThat(users.get(0).getFirstname(), is(carter.getFirstname()));
assertThat(users.get(2).getFirstname(), is(oliver.getFirstname()));
assertThat(users, hasItems(carter, dave, oliver));
}
}