Skip to content

Commit 25594c9

Browse files
committed
Document that fluent findBy(…) queries must return a result.
Closes #3294
1 parent acdc688 commit 25594c9

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
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

+11-3
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;
@@ -40,6 +41,7 @@
4041
import org.springframework.data.querydsl.EntityPathResolver;
4142
import org.springframework.data.querydsl.QSort;
4243
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
44+
import org.springframework.data.repository.query.FluentQuery;
4345
import org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery;
4446
import org.springframework.data.support.PageableExecutionUtils;
4547
import org.springframework.lang.Nullable;
@@ -235,8 +237,7 @@ public <S extends T, R> R findBy(Predicate predicate, Function<FetchableFluentQu
235237
};
236238

237239
FetchableFluentQueryByPredicate<T, T> fluentQuery = new FetchableFluentQueryByPredicate<>( //
238-
path,
239-
predicate, //
240+
path, predicate, //
240241
this.entityInformation, //
241242
finder, //
242243
scroll, //
@@ -246,7 +247,14 @@ public <S extends T, R> R findBy(Predicate predicate, Function<FetchableFluentQu
246247
entityManager, //
247248
getProjectionFactory());
248249

249-
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;
250258
}
251259

252260
@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
@@ -43,6 +43,7 @@
4343
import java.util.function.BiConsumer;
4444
import java.util.function.Function;
4545

46+
import org.springframework.dao.InvalidDataAccessApiUsageException;
4647
import org.springframework.data.domain.Example;
4748
import org.springframework.data.domain.KeysetScrollPosition;
4849
import org.springframework.data.domain.OffsetScrollPosition;
@@ -66,6 +67,7 @@
6667
import org.springframework.data.mapping.PropertyPath;
6768
import org.springframework.data.projection.ProjectionFactory;
6869
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
70+
import org.springframework.data.repository.query.FluentQuery;
6971
import org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery;
7072
import org.springframework.data.repository.query.ReturnedType;
7173
import org.springframework.data.support.PageableExecutionUtils;
@@ -542,7 +544,14 @@ private <S extends T, R> R doFindBy(Specification<T> spec, Class<T> domainClass,
542544
FetchableFluentQueryBySpecification<?, T> fluentQuery = new FetchableFluentQueryBySpecification<>(spec, domainClass,
543545
finder, scrollDelegate, this::count, this::exists, this.entityManager, getProjectionFactory());
544546

545-
return queryFunction.apply((FetchableFluentQuery<S>) fluentQuery);
547+
R result = queryFunction.apply((FetchableFluentQuery<S>) fluentQuery);
548+
549+
if (result instanceof FluentQuery<?>) {
550+
throw new InvalidDataAccessApiUsageException(
551+
"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");
552+
}
553+
554+
return result;
546555
}
547556

548557
@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

@@ -2406,6 +2407,14 @@ void findByFluentExampleWithSorting() {
24062407
assertThat(users).containsExactly(thirdUser, firstUser, fourthUser);
24072408
}
24082409

2410+
@Test // GH-3294
2411+
void findByFluentFailsReturningFluentQuery() {
2412+
2413+
User prototype = new User();
2414+
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class)
2415+
.isThrownBy(() -> repository.findBy(of(prototype), Function.identity()));
2416+
}
2417+
24092418
@Test // GH-2294
24102419
void findByFluentExampleFirstValue() {
24112420

@@ -2503,13 +2512,13 @@ void findByFluentExampleWithInterfaceBasedProjectionUsingSpEL() {
25032512
prototype.setFirstname("v");
25042513

25052514
List<UserProjectionUsingSpEL> users = repository.findBy(
2506-
of(prototype,
2507-
matching().withIgnorePaths("age", "createdAt", "active").withMatcher("firstname",
2508-
GenericPropertyMatcher::contains)), //
2509-
q -> q.as(UserProjectionUsingSpEL.class).all());
2515+
of(prototype,
2516+
matching().withIgnorePaths("age", "createdAt", "active").withMatcher("firstname",
2517+
GenericPropertyMatcher::contains)), //
2518+
q -> q.as(UserProjectionUsingSpEL.class).all());
25102519

25112520
assertThat(users).extracting(UserProjectionUsingSpEL::hello)
2512-
.contains(new GreetingsFrom().groot(firstUser.getFirstname()));
2521+
.contains(new GreetingsFrom().groot(firstUser.getFirstname()));
25132522
}
25142523

25152524
@Test // GH-2294

0 commit comments

Comments
 (0)