Skip to content

Commit 5deb3d6

Browse files
mp911dechristophstrobl
authored andcommitted
Document Querydsl annotation processor usage.
Closes: #4811 Original Pull Request: #4814
1 parent 1fc802d commit 5deb3d6

File tree

1 file changed

+104
-8
lines changed

1 file changed

+104
-8
lines changed

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

+104-8
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ 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.
214214

215215
[[mongodb.repositories.queries.type-safe]]
216-
== Type-safe Query Methods
216+
== Type-safe Query Methods with Querydsl
217217

218218
MongoDB repository and its reactive counterpart integrates with the http://www.querydsl.com/[Querydsl] project, which provides a way to perform type-safe queries.
219219

@@ -238,7 +238,7 @@ Imperative::
238238
+
239239
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
240240
----
241-
QPerson person = new QPerson("person");
241+
QPerson person = QPerson.person;
242242
List<Person> result = repository.findAll(person.address.zipCode.eq("C0123"));
243243
244244
Page<Person> page = repository.findAll(person.lastname.contains("a"),
@@ -255,7 +255,8 @@ Flux<Person> result = repository.findAll(person.address.zipCode.eq("C0123"));
255255
----
256256
======
257257

258-
`QPerson` is a class that is generated by the Java annotation post-processing tool.
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.
259260
It is a `Predicate` that lets you write type-safe queries.
260261
Notice that there are no strings in the query other than the `C0123` value.
261262

@@ -269,7 +270,7 @@ Imperative::
269270
----
270271
public interface QuerydslPredicateExecutor<T> {
271272
272-
T findOne(Predicate predicate);
273+
Optional<T> findOne(Predicate predicate);
273274
274275
List<T> findAll(Predicate predicate);
275276
@@ -281,9 +282,11 @@ public interface QuerydslPredicateExecutor<T> {
281282
282283
List<T> findAll(OrderSpecifier<?>... orders);
283284
284-
Long count(Predicate predicate);
285+
long count(Predicate predicate);
286+
287+
boolean exists(Predicate predicate);
285288
286-
Boolean exists(Predicate predicate);
289+
<S extends T, R> R findBy(Predicate predicate, Function<FluentQuery.FetchableFluentQuery<S>, R> queryFunction);
287290
}
288291
----
289292
@@ -306,6 +309,9 @@ interface ReactiveQuerydslPredicateExecutor<T> {
306309
Mono<Long> count(Predicate predicate);
307310
308311
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);
309315
}
310316
----
311317
======
@@ -320,7 +326,7 @@ Imperative::
320326
----
321327
interface PersonRepository extends MongoRepository<Person, String>, QuerydslPredicateExecutor<Person> {
322328
323-
// additional query methods go here
329+
// additional query methods go here
324330
}
325331
----
326332
@@ -332,10 +338,100 @@ Reactive::
332338
333339
interface PersonRepository extends ReactiveMongoRepository<Person, String>, ReactiveQuerydslPredicateExecutor<Person> {
334340
335-
// additional query methods go here
341+
// additional query methods go here
336342
}
337343
----
338344

339345
NOTE: Please note that joins (DBRef's) are not supported with Reactive MongoDB support.
340346
====
341347
======
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+
374+
<!-- Recommended: Exclude the mongo-java-driver to avoid version conflicts -->
375+
<exclusions>
376+
<exclusion>
377+
<groupId>org.mongodb</groupId>
378+
<artifactId>mongo-java-driver</artifactId>
379+
</exclusion>
380+
</exclusions>
381+
</dependency>
382+
383+
<dependency>
384+
<groupId>com.querydsl</groupId>
385+
<artifactId>querydsl-apt</artifactId>
386+
<version>${querydslVersion}</version>
387+
<scope>provided</scope>
388+
</dependency>
389+
</dependencies>
390+
391+
<build>
392+
<plugins>
393+
<plugin>
394+
<groupId>org.apache.maven.plugins</groupId>
395+
<artifactId>maven-compiler-plugin</artifactId>
396+
<configuration>
397+
<annotationProcessors>
398+
<annotationProcessor>
399+
org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor
400+
</annotationProcessor>
401+
</annotationProcessors>
402+
403+
<!-- Recommended: Some IDE's might require this configuration to include generated sources for IDE usage -->
404+
<generatedTestSourcesDirectory>target/generated-test-sources</generatedTestSourcesDirectory>
405+
<generatedSourcesDirectory>target/generated-sources</generatedSourcesDirectory>
406+
</configuration>
407+
</plugin>
408+
</plugins>
409+
</build>
410+
----
411+
412+
Gradle::
413+
+
414+
====
415+
[source,groovy,indent=0,subs="verbatim,quotes",role="secondary"]
416+
----
417+
dependencies {
418+
implementation 'com.querydsl:querydsl-mongodb:${querydslVersion}'
419+
420+
annotationProcessor 'com.querydsl:querydsl-apt:${querydslVersion}'
421+
annotationProcessor 'org.springframework.data:spring-data-mongodb'
422+
423+
testAnnotationProcessor 'com.querydsl:querydsl-apt:${querydslVersion}'
424+
testAnnotationProcessor 'org.springframework.data:spring-data-mongodb'
425+
}
426+
427+
tasks.withType(JavaCompile).configureEach {
428+
options.compilerArgs += [
429+
"-processor",
430+
"org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor"]
431+
}
432+
----
433+
434+
====
435+
======
436+
437+
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)