From 5deb3d6693a8b41d3e746460f0b52ae123008869 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 17 Oct 2024 09:16:18 +0200 Subject: [PATCH] Document Querydsl annotation processor usage. Closes: #4811 Original Pull Request: #4814 --- .../mongodb/repositories/repositories.adoc | 112 ++++++++++++++++-- 1 file changed, 104 insertions(+), 8 deletions(-) diff --git a/src/main/antora/modules/ROOT/pages/mongodb/repositories/repositories.adoc b/src/main/antora/modules/ROOT/pages/mongodb/repositories/repositories.adoc index 0ca6e271ea..3e29f3e950 100644 --- a/src/main/antora/modules/ROOT/pages/mongodb/repositories/repositories.adoc +++ b/src/main/antora/modules/ROOT/pages/mongodb/repositories/repositories.adoc @@ -213,7 +213,7 @@ Inside the test method, we use the repository to query the datastore. We hand the repository a `PageRequest` instance that requests the first page of `Person` objects at a page size of 10. [[mongodb.repositories.queries.type-safe]] -== Type-safe Query Methods +== Type-safe Query Methods with Querydsl MongoDB repository and its reactive counterpart integrates with the http://www.querydsl.com/[Querydsl] project, which provides a way to perform type-safe queries. @@ -238,7 +238,7 @@ Imperative:: + [source,java,indent=0,subs="verbatim,quotes",role="primary"] ---- -QPerson person = new QPerson("person"); +QPerson person = QPerson.person; List result = repository.findAll(person.address.zipCode.eq("C0123")); Page page = repository.findAll(person.lastname.contains("a"), @@ -255,7 +255,8 @@ Flux result = repository.findAll(person.address.zipCode.eq("C0123")); ---- ====== -`QPerson` is a class that is generated by the Java annotation post-processing tool. +`QPerson` is a class that is generated by the Java annotation processor. +See xref:#mongodb.repositories.queries.type-safe.apt[Setting up Annotation Processing] for how to setup Annotation Processing with your Build System. It is a `Predicate` that lets you write type-safe queries. Notice that there are no strings in the query other than the `C0123` value. @@ -269,7 +270,7 @@ Imperative:: ---- public interface QuerydslPredicateExecutor { - T findOne(Predicate predicate); + Optional findOne(Predicate predicate); List findAll(Predicate predicate); @@ -281,9 +282,11 @@ public interface QuerydslPredicateExecutor { List findAll(OrderSpecifier... orders); - Long count(Predicate predicate); + long count(Predicate predicate); + + boolean exists(Predicate predicate); - Boolean exists(Predicate predicate); + R findBy(Predicate predicate, Function, R> queryFunction); } ---- @@ -306,6 +309,9 @@ interface ReactiveQuerydslPredicateExecutor { Mono count(Predicate predicate); Mono exists(Predicate predicate); + + > P findBy(Predicate predicate, + Function, P> queryFunction); } ---- ====== @@ -320,7 +326,7 @@ Imperative:: ---- interface PersonRepository extends MongoRepository, QuerydslPredicateExecutor { - // additional query methods go here + // additional query methods go here } ---- @@ -332,10 +338,100 @@ Reactive:: interface PersonRepository extends ReactiveMongoRepository, ReactiveQuerydslPredicateExecutor { - // additional query methods go here + // additional query methods go here } ---- NOTE: Please note that joins (DBRef's) are not supported with Reactive MongoDB support. ==== ====== + +[[mongodb.repositories.queries.type-safe.apt]] +=== Setting up Annotation Processing + +To use Querydsl with Spring Data MongoDB, you need to set up annotation processing in your build system that generates the `Q` classes. +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. + +Spring Data MongoDB ships with an annotation processor javadoc:org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor[] that isn't registered by default. +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. +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. +Hence, you need to activate annotation processing in your build system. + +The following example shows how to set up annotation processing by mentioning dependencies and compiler config changes in Maven and Gradle: + +[tabs] +====== +Maven:: ++ +[source,xml,indent=0,subs="verbatim,quotes",role="primary"] +---- + + + com.querydsl + querydsl-mongodb + ${querydslVersion} + + + + + org.mongodb + mongo-java-driver + + + + + + com.querydsl + querydsl-apt + ${querydslVersion} + provided + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + + org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor + + + + + target/generated-test-sources + target/generated-sources + + + + +---- + +Gradle:: ++ +==== +[source,groovy,indent=0,subs="verbatim,quotes",role="secondary"] +---- +dependencies { + implementation 'com.querydsl:querydsl-mongodb:${querydslVersion}' + + annotationProcessor 'com.querydsl:querydsl-apt:${querydslVersion}' + annotationProcessor 'org.springframework.data:spring-data-mongodb' + + testAnnotationProcessor 'com.querydsl:querydsl-apt:${querydslVersion}' + testAnnotationProcessor 'org.springframework.data:spring-data-mongodb' +} + +tasks.withType(JavaCompile).configureEach { + options.compilerArgs += [ + "-processor", + "org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor"] +} +---- + +==== +====== + +Note that the setup above shows the simplest usage omitting any other options or dependencies that your project might require.