Add FluentQuery support to QueryByExampleExecutor and ReactiveQueryByExampleExecutor.

FluentQuery allows extending a query specification initially defined by a Example probe or a Querydsl Predicate and fetching the actual result through a functional programming model:

interface PersonRepository extends QuerydslPredicateExecutor<Person> {
  <S extends T, R> R findBy(Predicate predicate, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction);
}

PersonRepository repo = …;

List<PersonProjection> result = repo.findBy(QPerson.person.name.eq("Walter"), q -> q.sort(Sort.by("lastname")).as(PersonProjection.class).all());

Closes: #2228
Original pull request: #2421.
This commit is contained in:
Mark Paluch
2021-07-28 10:19:40 +02:00
parent f1f08933da
commit 511181193e
5 changed files with 303 additions and 0 deletions

View File

@@ -16,10 +16,12 @@
package org.springframework.data.querydsl;
import java.util.Optional;
import java.util.function.Function;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.FluentQuery;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
@@ -108,4 +110,15 @@ public interface QuerydslPredicateExecutor<T> {
* @return {@literal true} if the data store contains elements that match the given {@link Predicate}.
*/
boolean exists(Predicate predicate);
/**
* Returns entities matching the given {@link Predicate} applying the {@link Function queryFunction} that defines the
* query and its result type.
*
* @param predicate must not be {@literal null}.
* @param queryFunction the query function defining projection, sorting, and the result type
* @return all entities matching the given {@link Predicate}.
* @since 2.6
*/
<S extends T, R> R findBy(Predicate predicate, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction);
}

View File

@@ -18,7 +18,12 @@ package org.springframework.data.querydsl;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.function.Function;
import org.reactivestreams.Publisher;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.FluentQuery;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;
@@ -127,4 +132,16 @@ public interface ReactiveQuerydslPredicateExecutor<T> {
* @throws IllegalArgumentException if the required parameter is {@literal null}.
*/
Mono<Boolean> exists(Predicate predicate);
/**
* Returns entities matching the given {@link Predicate} applying the {@link Function queryFunction} that defines the
* query and its result type.
*
* @param predicate must not be {@literal null}.
* @param queryFunction the query function defining projection, sorting, and the result type
* @return all entities matching the given {@link Predicate}.
* @since 2.6
*/
<S extends T, R, P extends Publisher<R>> P findBy(Predicate predicate,
Function<FluentQuery.ReactiveFluentQuery<S>, P> queryFunction);
}