28

I'm trying to integrate QueryDSL to my existing project with Spring Data, I've tried different samples and now I've decided to stick to this one Advanced Spring Data JPA - Specifications and Querydsl.

Problem: when I run the project as Maven generate-sources I get this error

error: Annotation processor 'com.mysema.query.apt.jpa.JPAAnnotationProcessor' not found

I'm adding this plugin to my pom.xml as the blog post indicates:

<plugin>
  <groupId>com.mysema.maven</groupId>
  <artifactId>maven-apt-plugin</artifactId>
  <version>1.0</version>
  <executions>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>process</goal>
      </goals>
      <configuration>
        <outputDirectory>target/generated-sources</outputDirectory>
        <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
      </configuration>
    </execution>
  </executions>
</plugin>

and the dependency:

<dependency>
     <groupId>com.mysema.querydsl</groupId>
     <artifactId>querydsl-sql</artifactId>
     <version>3.6.9</version>
</dependency>

Can anyone point me in the right direction on how to solve this or how to properly integrate QueryDSL to my project ? Thanks in advance!

M.Octavio
  • 1,780
  • 2
  • 25
  • 39

3 Answers3

51

The way I could make this work was using the com.querydsl.apt.jpa.JPAAnnotationProcessor instead of the com.mysema.query.apt.jpa.JPAAnnotationProcessor and by changing the dependencies as follow:

<dependency>
  <groupId>com.querydsl</groupId>
  <artifactId>querydsl-apt</artifactId>
  <version>4.0.6</version>
</dependency>
<dependency>
  <groupId>com.querydsl</groupId>
  <artifactId>querydsl-jpa</artifactId>
  <version>4.0.6</version>
</dependency>

The plugin end up like this:

<plugin>
  <groupId>com.mysema.maven</groupId>
  <artifactId>apt-maven-plugin</artifactId>
  <version>1.1.3</version>
  <executions>
    <execution>
      <phase>generate-sources</phase>
      <goals>
        <goal>process</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.directory}/generated-sources</outputDirectory>
         <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
      </configuration>
    </execution>
  </executions>
</plugin>

I also executed in the command line at the projects root mvn eclipse:eclipse to update Eclipse to include the generated sources.

Update:

Replaced the plugin maven-apt-plugin for apt-maven-plugin and changed version to 1.1.3

M.Octavio
  • 1,780
  • 2
  • 25
  • 39
  • Thank you for your example, works perfectly. Note: to avoid an exception like **"could not load entity class [...] NoClassDefFoundError: com/querydsl/core/types/dsl/EntityPathBase"**, it seems to be important to use the root **${project.build.directory}/generated-sources** and not a subdirectory in generated sources. – Chavjoh Aug 07 '16 at 20:49
  • See related answers in [this question](https://stackoverflow.com/questions/49597466) with the example for lombok and mapstruct – Sam Dec 21 '20 at 20:31
  • For anyone who is reading, if you have multiple module maven structure, the entities that will be processed are only the ones in the module where you define plugin. – Matjaz Nov 30 '21 at 12:49
2

Instead of explicitly configuring the apt-maven-plugin just add the following dependency to the project (note the jpa classifier):

<dependency>
  <groupId>com.querydsl</groupId>
  <artifactId>querydsl-apt</artifactId>
  <version>${querydsl.version}</version>
  <classifier>jpa</classifier>
</dependency>

In case of Jakarta Persistence:

<dependency>
  <groupId>com.querydsl</groupId>
  <artifactId>querydsl-apt</artifactId>
  <version>${querydsl.version}</version>
  <classifier>jakarta</classifier>
</dependency>

Also, if you want more Querydsl features than what QuerydslPredicateExecutor has to offer check out https://github.com/infobip/infobip-spring-data-querydsl#JPA.

Lovro Pandžić
  • 5,920
  • 4
  • 43
  • 51
0

I had to add the following to fix it.

<querydsl.version>5.0.0</querydsl.version>

<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>

<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
</dependency>

<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-core</artifactId>
<version>${querydsl.version}</version>
</dependency>
Smart Coder
  • 1,435
  • 19
  • 19