Complete description
Working example
Generate QueryDSL classes
In this example we have two Maven modules:
entities
, which contains only JPA entities,jpa
, which contains the EntityManagerFactory initialization and calls to QueryDSL's JPAQueryFactory in order to perform a database requests.The entities
module is meant to not have any dependency on QueryDSL, and jpa
is supposed to include the entities
dependency in order to generate the QueryDSL query type classes (Q* classes). This is supposed to be done by the querydsl-maven-plugin
but it currently does not work (see querydsl#3462).
Instead, the querydsl-apt
dependency with the jakarta
classifier was added to the entities
module, so that query type classes are generated and can be used by the jpa
module.
querydsl-maven-plugin
The querydsl-maven-plugin
should be used if the JPA annotated sources are not available. Here is a configuration example which works with
Jakarta (note the usage of the javax.persistence-api
, necessary due to an issue with the plugin).
<plugin>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-maven-plugin</artifactId>
<version>${querydsl.version}</version>
<dependencies>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>jpa-export</goal>
</goals>
<configuration>
<targetFolder>target/generated-sources/java</targetFolder>
<packages>
<package>edu.self.nyg.example.querydsl.entity</package>
</packages>
</configuration>
</execution>
</executions>
</plugin>
maven-compiler-plugin
If the configuration of the maven-compiler-plugin
needs to be modified (e.g. to add lombok annotation processing with mapstruct), it is
necessary to add the last two dependencies in the annotation processor path:
<configuration>
<annotationProcessorPaths>
<!-- To process lombok annotations -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<!-- To generate QueryDSL type classes -->
<path>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>${jakarta.persistence-api.version}</version>
</path>
<path>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
<classifier>jakarta</classifier>
</path>
</annotationProcessorPaths>
</configuration>