Skip to content

Commit 9bc7dac

Browse files
committed
Document that fluent findBy(…) queries must return a result.
Closes #3294
1 parent d0c63eb commit 9bc7dac

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/JpaSpecificationExecutor.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Optional;
2424
import java.util.function.Function;
2525

26+
import org.springframework.dao.InvalidDataAccessApiUsageException;
2627
import org.springframework.data.domain.Page;
2728
import org.springframework.data.domain.Pageable;
2829
import org.springframework.data.domain.Sort;
@@ -122,11 +123,16 @@ public interface JpaSpecificationExecutor<T> {
122123
/**
123124
* Returns entities matching the given {@link Specification} applying the {@code queryFunction} that defines the query
124125
* and its result type.
126+
* <p>
127+
* The query object used with {@code queryFunction} is only valid inside the {@code findBy(…)} method call. This
128+
* requires the query function to return a query result and not the {@link FluentQuery} object itself to ensure the
129+
* query is executed inside the {@code findBy(…)} method.
125130
*
126131
* @param spec must not be null.
127132
* @param queryFunction the query function defining projection, sorting, and the result type
128-
* @return all entities matching the given Example.
133+
* @return all entities matching the given specification.
129134
* @since 3.0
135+
* @throws InvalidDataAccessApiUsageException if the query function returns the {@link FluentQuery} instance.
130136
*/
131137
<S extends T, R> R findBy(Specification<T> spec, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction);
132138

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/QuerydslJpaPredicateExecutor.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.function.Function;
2525

2626
import org.springframework.dao.IncorrectResultSizeDataAccessException;
27+
import org.springframework.dao.InvalidDataAccessApiUsageException;
2728
import org.springframework.data.domain.KeysetScrollPosition;
2829
import org.springframework.data.domain.OffsetScrollPosition;
2930
import org.springframework.data.domain.Page;
@@ -41,6 +42,7 @@
4142
import org.springframework.data.querydsl.EntityPathResolver;
4243
import org.springframework.data.querydsl.QSort;
4344
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
45+
import org.springframework.data.repository.query.FluentQuery;
4446
import org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery;
4547
import org.springframework.data.support.PageableExecutionUtils;
4648
import org.springframework.lang.Nullable;
@@ -245,7 +247,14 @@ public <S extends T, R> R findBy(Predicate predicate, Function<FetchableFluentQu
245247
entityManager, //
246248
getProjectionFactory());
247249

248-
return queryFunction.apply((FetchableFluentQuery<S>) fluentQuery);
250+
R result = queryFunction.apply((FetchableFluentQuery<S>) fluentQuery);
251+
252+
if (result instanceof FluentQuery<?>) {
253+
throw new InvalidDataAccessApiUsageException(
254+
"findBy(…) queries must result a query result and not the FluentQuery object to ensure that queries are executed within the scope of the findBy(…) method");
255+
}
256+
257+
return result;
249258
}
250259

251260
@Override

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.util.function.BiConsumer;
4343
import java.util.function.Function;
4444

45+
import org.springframework.dao.InvalidDataAccessApiUsageException;
4546
import org.springframework.data.domain.Example;
4647
import org.springframework.data.domain.KeysetScrollPosition;
4748
import org.springframework.data.domain.OffsetScrollPosition;
@@ -62,6 +63,7 @@
6263
import org.springframework.data.jpa.support.PageableUtils;
6364
import org.springframework.data.projection.ProjectionFactory;
6465
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
66+
import org.springframework.data.repository.query.FluentQuery;
6567
import org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery;
6668
import org.springframework.data.support.PageableExecutionUtils;
6769
import org.springframework.data.util.ProxyUtils;
@@ -534,7 +536,14 @@ private <S extends T, R> R doFindBy(Specification<T> spec, Class<T> domainClass,
534536
FetchableFluentQueryBySpecification<?, T> fluentQuery = new FetchableFluentQueryBySpecification<>(spec, domainClass,
535537
finder, scrollDelegate, this::count, this::exists, this.entityManager, getProjectionFactory());
536538

537-
return queryFunction.apply((FetchableFluentQuery<S>) fluentQuery);
539+
R result = queryFunction.apply((FetchableFluentQuery<S>) fluentQuery);
540+
541+
if (result instanceof FluentQuery<?>) {
542+
throw new InvalidDataAccessApiUsageException(
543+
"findBy(…) queries must result a query result and not the FluentQuery object to ensure that queries are executed within the scope of the findBy(…) method");
544+
}
545+
546+
return result;
538547
}
539548

540549
@Override

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.util.Map;
4343
import java.util.Optional;
4444
import java.util.Set;
45+
import java.util.function.Function;
4546
import java.util.stream.IntStream;
4647
import java.util.stream.Stream;
4748

@@ -2355,6 +2356,14 @@ void findByFluentExampleWithSorting() {
23552356
assertThat(users).containsExactly(thirdUser, firstUser, fourthUser);
23562357
}
23572358

2359+
@Test // GH-3294
2360+
void findByFluentFailsReturningFluentQuery() {
2361+
2362+
User prototype = new User();
2363+
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class)
2364+
.isThrownBy(() -> repository.findBy(of(prototype), Function.identity()));
2365+
}
2366+
23582367
@Test // GH-2294
23592368
void findByFluentExampleFirstValue() {
23602369

@@ -2452,13 +2461,13 @@ void findByFluentExampleWithInterfaceBasedProjectionUsingSpEL() {
24522461
prototype.setFirstname("v");
24532462

24542463
List<UserProjectionUsingSpEL> users = repository.findBy(
2455-
of(prototype,
2456-
matching().withIgnorePaths("age", "createdAt", "active").withMatcher("firstname",
2457-
GenericPropertyMatcher::contains)), //
2458-
q -> q.as(UserProjectionUsingSpEL.class).all());
2464+
of(prototype,
2465+
matching().withIgnorePaths("age", "createdAt", "active").withMatcher("firstname",
2466+
GenericPropertyMatcher::contains)), //
2467+
q -> q.as(UserProjectionUsingSpEL.class).all());
24592468

24602469
assertThat(users).extracting(UserProjectionUsingSpEL::hello)
2461-
.contains(new GreetingsFrom().groot(firstUser.getFirstname()));
2470+
.contains(new GreetingsFrom().groot(firstUser.getFirstname()));
24622471
}
24632472

24642473
@Test // GH-2294

0 commit comments

Comments
 (0)