From d3c00a93c0e57a2357f74c1e28774f7b6883ead1 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 19 Jan 2021 10:46:55 +0100 Subject: [PATCH] Update QBE Documentation section. This commit adds a note explaining scenarios suitable for an UntypedExampleMatcher. Closes: #3474 Original pull request: #3538. --- .../data/mongodb/core/query/Criteria.java | 7 ++++- .../mongodb/repository/MongoRepository.java | 25 ++++++++++++---- .../repository/ReactiveMongoRepository.java | 29 +++++++++++++++---- .../asciidoc/reference/query-by-example.adoc | 7 +++++ 4 files changed, 56 insertions(+), 12 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java index 7f8ec91fe..496e292a3 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java @@ -124,7 +124,12 @@ public class Criteria implements CriteriaDefinition { } /** - * Static factory method to create a {@link Criteria} matching an example object. + * Static factory method to create a {@link Criteria} matching an example object.
+ * By default the {@link Example} uses typed matching restricting it to probe assignable types. For example, when + * sticking with the default type key ({@code _class}), the query has restrictions such as + * _class : { $in : [com.acme.Person] } .
+ * To avoid the above mentioned type restriction use an {@link UntypedExampleMatcher} with + * {@link Example#of(Object, org.springframework.data.domain.ExampleMatcher)}. * * @param example must not be {@literal null}. * @return new instance of {@link Criteria}. diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepository.java index 144bb23af..24e87121e 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/MongoRepository.java @@ -78,16 +78,31 @@ public interface MongoRepository extends PagingAndSortingRepository List insert(Iterable entities); - /* - * (non-Javadoc) + /** + * Returns all entities matching the given {@link Example}. In case no match could be found an empty {@link List} is + * returned.
+ * By default the {@link Example} uses typed matching restricting it to probe assignable types. For example, when + * sticking with the default type key ({@code _class}), the query has restrictions such as + * _class : { $in : [com.acme.Person] }.
+ * To avoid the above mentioned type restriction use an {@link org.springframework.data.mongodb.core.query.UntypedExampleMatcher} with + * {@link Example#of(Object, org.springframework.data.domain.ExampleMatcher)}. + * * @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example) */ @Override List findAll(Example example); - /* - * (non-Javadoc) - * @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example, org.springframework.data.domain.Sort) + /** + * Returns all entities matching the given {@link Example} applying the given {@link Sort}. In case no match could be + * found an empty {@link List} is returned.
+ * By default the {@link Example} uses typed matching restricting it to probe assignable types. For example, when + * sticking with the default type key ({@code _class}), the query has restrictions such as + * _class : { $in : [com.acme.Person] }.
+ * To avoid the above mentioned type restriction use an {@link org.springframework.data.mongodb.core.query.UntypedExampleMatcher} with + * {@link Example#of(Object, org.springframework.data.domain.ExampleMatcher)}. + * + * @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example, + * org.springframework.data.domain.Sort) */ @Override List findAll(Example example, Sort sort); diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/ReactiveMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/ReactiveMongoRepository.java index 8ea253f60..a9c62a50a 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/ReactiveMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/ReactiveMongoRepository.java @@ -64,16 +64,33 @@ public interface ReactiveMongoRepository extends ReactiveSortingRepositor */ Flux insert(Publisher entities); - /* - * (non-Javadoc) - * @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example) + /** + * Returns all entities matching the given {@link Example}. In case no match could be found an empty {@link Flux} is + * returned.
+ * By default the {@link Example} uses typed matching restricting it to probe assignable types. For example, when + * sticking with the default type key ({@code _class}), the query has restrictions such as + * _class : { $in : [com.acme.Person] }.
+ * To avoid the above mentioned type restriction use an {@link org.springframework.data.mongodb.core.query.UntypedExampleMatcher} with + * {@link Example#of(Object, org.springframework.data.domain.ExampleMatcher)}. + * + * @see org.springframework.data.repository.query.ReactiveQueryByExampleExecutor#findAll(org.springframework.data.domain.Example) */ + @Override Flux findAll(Example example); - /* - * (non-Javadoc) - * @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example, org.springframework.data.domain.Sort) + /** + * Returns all entities matching the given {@link Example} applying the given {@link Sort}. In case no match could be + * found an empty {@link Flux} is returned.
+ * By default the {@link Example} uses typed matching restricting it to probe assignable types. For example, when + * sticking with the default type key ({@code _class}), the query has restrictions such as + * _class : { $in : [com.acme.Person] }.
+ * To avoid the above mentioned type restriction use an {@link org.springframework.data.mongodb.core.query.UntypedExampleMatcher} with + * {@link Example#of(Object, org.springframework.data.domain.ExampleMatcher)}. + * + * @see org.springframework.data.repository.query.ReactiveQueryByExampleExecutor#findAll(org.springframework.data.domain.Example, + * org.springframework.data.domain.Sort) */ + @Override Flux findAll(Example example, Sort sort); } diff --git a/src/main/asciidoc/reference/query-by-example.adoc b/src/main/asciidoc/reference/query-by-example.adoc index d1f5ce5aa..342ade291 100644 --- a/src/main/asciidoc/reference/query-by-example.adoc +++ b/src/main/asciidoc/reference/query-by-example.adoc @@ -97,3 +97,10 @@ Query query = new Query(new Criteria().alike(example)); List result = template.find(query, Person.class); ---- ==== + +[NOTE] +==== +`UntypedExampleMatcher` is likely the right choice for you if you are storing different entities within a single collection or opted out of writing <>. + +Also, keep in mind that using `@TypeAlias` requires eager initialization of the `MappingContext`. To do so, configure `initialEntitySet` to to ensure proper alias resolution for read operations. +====