Skip to content

Commit 2874b1e

Browse files
committed
Refine Querydsl documentation.
Closes #4894
1 parent 9840557 commit 2874b1e

File tree

4 files changed

+238
-228
lines changed

4 files changed

+238
-228
lines changed

src/main/antora/modules/ROOT/nav.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
** xref:repositories/core-concepts.adoc[]
4141
** xref:repositories/definition.adoc[]
4242
** xref:mongodb/repositories/repositories.adoc[]
43+
** xref:repositories/core-extensions.adoc[]
4344
** xref:repositories/create-instances.adoc[]
4445
** xref:repositories/query-methods-details.adoc[]
4546
** xref:mongodb/repositories/query-methods.adoc[]

src/main/antora/modules/ROOT/pages/mongodb.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Includes integrated object mapping between documents and POJOs.
1313
* xref:mongodb/lifecycle-events.adoc[Persistence and mapping lifecycle events].
1414
* xref:mongodb/template-query-operations.adoc[Java-based Query, Criteria, and Update DSLs].
1515
* Automatic implementation of xref:repositories.adoc[Repository interfaces], including support for custom query methods.
16-
* xref:mongodb/repositories/repositories.adoc#mongodb.repositories.queries.type-safe[QueryDSL integration] to support type-safe queries.
16+
* xref:repositories/core-extensions.adoc#mongodb.repositories.queries.type-safe[QueryDSL integration] to support type-safe queries.
1717
* xref:mongodb/client-session-transactions.adoc[Multi-Document Transactions].
1818
* xref:mongodb/template-query-operations.adoc#mongo.geo-json[GeoSpatial integration].
1919

src/main/antora/modules/ROOT/pages/mongodb/repositories/repositories.adoc

-226
Original file line numberDiff line numberDiff line change
@@ -211,229 +211,3 @@ class PersonRepositoryTests {
211211
The preceding example creates an application context with Spring's unit test support, which performs annotation-based dependency injection into test cases.
212212
Inside the test method, we use the repository to query the datastore.
213213
We hand the repository a `PageRequest` instance that requests the first page of `Person` objects at a page size of 10.
214-
215-
[[mongodb.repositories.queries.type-safe]]
216-
== Type-safe Query Methods with Querydsl
217-
218-
MongoDB repository and its reactive counterpart integrates with the http://www.querydsl.com/[Querydsl] project, which provides a way to perform type-safe queries.
219-
220-
[quote, Querydsl Team]
221-
Instead of writing queries as inline strings or externalizing them into XML files they are constructed via a fluent API.
222-
223-
It provides the following features:
224-
225-
* Code completion in the IDE (all properties, methods, and operations can be expanded in your favorite Java IDE).
226-
* Almost no syntactically invalid queries allowed (type-safe on all levels).
227-
* Domain types and properties can be referenced safely -- no strings involved!
228-
* Adapts better to refactoring changes in domain types.
229-
* Incremental query definition is easier.
230-
231-
See the http://www.querydsl.com/static/querydsl/latest/reference/html/[QueryDSL documentation] for how to bootstrap your environment for APT-based code generation using Maven or Ant.
232-
233-
QueryDSL lets you write queries such as the following:
234-
235-
[tabs]
236-
======
237-
Imperative::
238-
+
239-
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
240-
----
241-
QPerson person = QPerson.person;
242-
List<Person> result = repository.findAll(person.address.zipCode.eq("C0123"));
243-
244-
Page<Person> page = repository.findAll(person.lastname.contains("a"),
245-
PageRequest.of(0, 2, Direction.ASC, "lastname"));
246-
----
247-
248-
Reactive::
249-
+
250-
[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
251-
----
252-
QPerson person = QPerson.person;
253-
254-
Flux<Person> result = repository.findAll(person.address.zipCode.eq("C0123"));
255-
----
256-
======
257-
258-
`QPerson` is a class that is generated by the Java annotation processor.
259-
See xref:#mongodb.repositories.queries.type-safe.apt[Setting up Annotation Processing] for how to setup Annotation Processing with your Build System.
260-
It is a `Predicate` that lets you write type-safe queries.
261-
Notice that there are no strings in the query other than the `C0123` value.
262-
263-
You can use the generated `Predicate` class by using the `QuerydslPredicateExecutor` / `ReactiveQuerydslPredicateExecutor` interface, which the following listing shows:
264-
265-
[tabs]
266-
======
267-
Imperative::
268-
+
269-
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
270-
----
271-
public interface QuerydslPredicateExecutor<T> {
272-
273-
Optional<T> findOne(Predicate predicate);
274-
275-
List<T> findAll(Predicate predicate);
276-
277-
List<T> findAll(Predicate predicate, Sort sort);
278-
279-
List<T> findAll(Predicate predicate, OrderSpecifier<?>... orders);
280-
281-
Page<T> findAll(Predicate predicate, Pageable pageable);
282-
283-
List<T> findAll(OrderSpecifier<?>... orders);
284-
285-
long count(Predicate predicate);
286-
287-
boolean exists(Predicate predicate);
288-
289-
<S extends T, R> R findBy(Predicate predicate, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction);
290-
}
291-
----
292-
293-
Reactive::
294-
+
295-
[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
296-
----
297-
interface ReactiveQuerydslPredicateExecutor<T> {
298-
299-
Mono<T> findOne(Predicate predicate);
300-
301-
Flux<T> findAll(Predicate predicate);
302-
303-
Flux<T> findAll(Predicate predicate, Sort sort);
304-
305-
Flux<T> findAll(Predicate predicate, OrderSpecifier<?>... orders);
306-
307-
Flux<T> findAll(OrderSpecifier<?>... orders);
308-
309-
Mono<Long> count(Predicate predicate);
310-
311-
Mono<Boolean> exists(Predicate predicate);
312-
313-
<S extends T, R, P extends Publisher<R>> P findBy(Predicate predicate,
314-
Function<FluentQuery.ReactiveFluentQuery<S>, P> queryFunction);
315-
}
316-
----
317-
======
318-
319-
To use this in your repository implementation, add it to the list of repository interfaces from which your interface inherits, as the following example shows:
320-
321-
[tabs]
322-
======
323-
Imperative::
324-
+
325-
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
326-
----
327-
interface PersonRepository extends MongoRepository<Person, String>, QuerydslPredicateExecutor<Person> {
328-
329-
// additional query methods go here
330-
}
331-
----
332-
333-
Reactive::
334-
+
335-
====
336-
[source,java,indent=0,subs="verbatim,quotes",role="secondary"]
337-
----
338-
339-
interface PersonRepository extends ReactiveMongoRepository<Person, String>, ReactiveQuerydslPredicateExecutor<Person> {
340-
341-
// additional query methods go here
342-
}
343-
----
344-
345-
NOTE: Please note that joins (DBRef's) are not supported with Reactive MongoDB support.
346-
====
347-
======
348-
349-
[[mongodb.repositories.queries.type-safe.apt]]
350-
=== Setting up Annotation Processing
351-
352-
To use Querydsl with Spring Data MongoDB, you need to set up annotation processing in your build system that generates the `Q` classes.
353-
While you could write the `Q` classes by hand, it is recommended to use the Querydsl annotation processor to generate them for you to keep your `Q` classes in sync with your domain model.
354-
355-
Spring Data MongoDB ships with an annotation processor javadoc:org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor[] that isn't registered by default.
356-
Typically, annotation processors are registered through Java's service loader via `META-INF/services/javax.annotation.processing.Processor` that also activates these once you have them on the class path.
357-
Most Spring Data users do not use Querydsl, so it does not make sense to require additional mandatory dependencies for projects that would not benefit from Querydsl.
358-
Hence, you need to activate annotation processing in your build system.
359-
360-
The following example shows how to set up annotation processing by mentioning dependencies and compiler config changes in Maven and Gradle:
361-
362-
[tabs]
363-
======
364-
Maven::
365-
+
366-
[source,xml,indent=0,subs="verbatim,quotes",role="primary"]
367-
----
368-
<dependencies>
369-
<dependency>
370-
<groupId>com.querydsl</groupId>
371-
<artifactId>querydsl-mongodb</artifactId>
372-
<version>${querydslVersion}</version>
373-
<classifier>jakarta</classifier>
374-
375-
<!-- Recommended: Exclude the mongo-java-driver to avoid version conflicts -->
376-
<exclusions>
377-
<exclusion>
378-
<groupId>org.mongodb</groupId>
379-
<artifactId>mongo-java-driver</artifactId>
380-
</exclusion>
381-
</exclusions>
382-
</dependency>
383-
384-
<dependency>
385-
<groupId>com.querydsl</groupId>
386-
<artifactId>querydsl-apt</artifactId>
387-
<version>${querydslVersion}</version>
388-
<classifier>jakarta</classifier>
389-
<scope>provided</scope>
390-
</dependency>
391-
</dependencies>
392-
393-
<build>
394-
<plugins>
395-
<plugin>
396-
<groupId>org.apache.maven.plugins</groupId>
397-
<artifactId>maven-compiler-plugin</artifactId>
398-
<configuration>
399-
<annotationProcessors>
400-
<annotationProcessor>
401-
org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor
402-
</annotationProcessor>
403-
</annotationProcessors>
404-
405-
<!-- Recommended: Some IDE's might require this configuration to include generated sources for IDE usage -->
406-
<generatedTestSourcesDirectory>target/generated-test-sources</generatedTestSourcesDirectory>
407-
<generatedSourcesDirectory>target/generated-sources</generatedSourcesDirectory>
408-
</configuration>
409-
</plugin>
410-
</plugins>
411-
</build>
412-
----
413-
414-
Gradle::
415-
+
416-
====
417-
[source,groovy,indent=0,subs="verbatim,quotes",role="secondary"]
418-
----
419-
dependencies {
420-
implementation 'com.querydsl:querydsl-mongodb:${querydslVersion}:jakarta'
421-
422-
annotationProcessor 'com.querydsl:querydsl-apt:${querydslVersion}:jakarta'
423-
annotationProcessor 'org.springframework.data:spring-data-mongodb'
424-
425-
testAnnotationProcessor 'com.querydsl:querydsl-apt:${querydslVersion}'
426-
testAnnotationProcessor 'org.springframework.data:spring-data-mongodb'
427-
}
428-
429-
tasks.withType(JavaCompile).configureEach {
430-
options.compilerArgs += [
431-
"-processor",
432-
"org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor"]
433-
}
434-
----
435-
436-
====
437-
======
438-
439-
Note that the setup above shows the simplest usage omitting any other options or dependencies that your project might require.

0 commit comments

Comments
 (0)