16

I have the following class and I would like to try out querydsl and make some basic queries. Using intelliJ 2017.3, no class QUser is generated. I've tried googling my problem and each SO answer seems to offer a different solution (some didn't work and some I did not understand cause I've never used this things before) and most tutorials seem to do completely different stuff.

I have tried making queries using whatever Spring Boot seems to have built-in (no idea, simply seems to work, but it's too basic from the looks of it) and the queries work just fine so I'm guessing it's some configuration issue (I'm a maven and spring noob).

// User.java
@Entity
@Table(name = "symptoms")
public class Symptom
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotNull
    private String name;

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }
}

I have added these things to the pom.xml:

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

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

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.25</version>
    </dependency>



    <plugin>
            <groupId>com.mysema.maven</groupId>
            <artifactId>apt-maven-plugin</artifactId>
            <version>1.1.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/generated-sources/java</outputDirectory>
                        <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Xzenon
  • 1,193
  • 2
  • 15
  • 37

3 Answers3

30

You can fix the problem by: Open target folder --> Right click to generated-sources folder -> Mark Directory As -> Generated sources root.

After correctly marking the generated sources folder it worked without any problems for me. If IntelliJ doesn't instantly recognize the marked folder and the error still persists, mvn clean install usually helps.

John
  • 401
  • 4
  • 6
0

My solution with setup querydsl 5.0.0, spring boot 3.1.3, without com.mysema.maven:apt-maven-plugin. What is important from Q-classe generation point of view is annotationProcessorPaths in org.apache.maven.plugins:maven-compiler-plugin configuration:

<properties>
    ...
    <java.version>17</java.version>
    <maven-compiler-plugin.version>3.11.0</maven-compiler-plugin.version>
    <querydsl.version>5.0.0</querydsl.version>
    <jakarta-persistence-api.version>3.1.0</jakarta-persistence-api.version>
    ...
</properties>

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

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <encoding>UTF-8</encoding>
                <annotationProcessorPaths>
                    ...  
                    <path>
                        <groupId>com.querydsl</groupId>
                        <artifactId>querydsl-apt</artifactId>
                        <version>${querydsl.version}</version>
                        <classifier>jakarta</classifier>
                    </path>
                    <path>
                        <groupId>jakarta.persistence</groupId>
                        <artifactId>jakarta.persistence-api</artifactId>
                        <version>3.1.0</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
        ...
    </plugins>
</build>
bladekp
  • 1,529
  • 22
  • 29
-1

I have managed to generate the Q classes using good ol' code. Just call this once and the classes will be generated in target/generated-sources/java. You can change this in the second-to-last line.

GenericExporter exporter = new GenericExporter();
exporter.setKeywords(Keywords.JPA);
exporter.setEntityAnnotation(Entity.class);
exporter.setEmbeddableAnnotation(Embeddable.class);
exporter.setEmbeddedAnnotation(Embedded.class);
exporter.setSupertypeAnnotation(MappedSuperclass.class);
exporter.setSkipAnnotation(Transient.class);
exporter.setTargetFolder(new File("target/generated-sources/java"));
exporter.export(ApplicationClass.class.getPackage());

In the last line, ApplicationClass is the class that starts the spring application.

Xzenon
  • 1,193
  • 2
  • 15
  • 37
  • 2
    why not use `mvn clean compile -DskipTests` in Terminal ? – Cataclysm Jul 08 '18 at 14:33
  • Sorry but you don't need own code to generate qclass. Something wrong on your configuration. Your ```@Table``` annotations come from ```javax.presistence.*```? What happens when you run ```mvn clean compile``` on your terminal? On intellij general settings, **Build, Execution, Deployment > Compiler > Annotation Processors**, what values do you have (this is only in case that terminal works but not on intellij)? – Sergio Gragera Jan 10 '19 at 07:51
  • @SergioGragera It's been over half a year since I've last worked on this (it was a university project), but I pulled it again on my machine. `mvn clean compile` compiles the source files to /target/classes and says BUILD SUCCESS. In IntelliJ I get two categories on the left: Default and Maven default annotations. My project is under Maven default annotations. – Xzenon Jan 10 '19 at 17:33
  • Maven says SUCCESS, but it must says something more. What about lines about apt execution on mvn clean compile? Have you verified that @Table come from javax.persistence? – Sergio Gragera Jan 10 '19 at 18:09
  • @SergioGragera `@Table` is indeed form javax.persistence. The command output in the terminal is as follows: `clean` - deleting target; `resources` - copying 1 resource, copying 13 resources; `compile` - recompiling the module, compiling 33 source files; BUILD SUCCESS - total time, finished at. I hope this is what you meant. – Xzenon Jan 10 '19 at 18:23
  • No, the output says something as starting apt-plugin execution. By other hand if you delete the tag ouputDirectory what happens? You are sure that not exists qclasses at target? – Sergio Gragera Jan 10 '19 at 19:01
  • Fi, i'm using ``mvn generate-sources``in IntelliJ (with the "CTRL CTRL" command), it's faster than mvn install for generating QueryDSL metamodel – Mickaël B. Jul 30 '20 at 15:18