Query DSL Q type classes not generated
Asked Answered
C

6

6

I am trying to use QueryDSL in my eclipse maven project. These are the dependencies.

<properties>
        <!-- The main class to start by executing java -jar -->
        <start-class>my.app.market.DBApp</start-class>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <querydsl.version>4.1.4</querydsl.version>
        <apt-maven-plugin.version>1.1.3</apt-maven-plugin.version>

</properties>

<dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <version>4.1.4</version>
            <scope>provided</scope>
        </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.6.1</version>
        </dependency>

<build>
        <plugins>
            <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</outputDirectory>
                            <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

After this I try to write the queries.

@Repository
public class QueryDSLRepo {

    @PersistenceContext
    private EntityManager em;

    public ReportingParamDAO save(final ReportingParamDAO reportingParamDAO) {
        em.persist(reportingParamDAO);
        return reportingParamDAO;
    }

    public List<ReportingParamDAO> findreportingParamDAOsByIdQueryDSL(final Integer id) {
        final JPAQuery<ReportingParamDAO> query = new JPAQuery<>(em);
        final QReportingParamDAO reportingParamDAO = QReportingParamDAO.reportingParamDAO;

        return query.from(reportingParamDAO).where(reportingParamDAO.id.eq(id)).fetch();
    }


}

But I get the error

QReportingParamDAO cannot be resolved to a type

Note: ReportingParamDAO is an entity class.

This means that the Q type class for my DAO is not generated. I am not sure why it wasn't generated. Do I need to do something else? I came across this post but the user is working on IntelliJ and I can't seem to make it work in my case. Can someone please help me. Thanks !!

Carrier answered 21/8, 2017 at 9:49 Comment(3)
Have you tried debugging via maven debug output like mvn -X clean install. That will give you better info on what is happening in that plugin. If possible, can you post the relevant console output for this plugin?Sectarianism
@Sectarianism Thanks for the reply. Without using the QReportingParamDAO in the class, when I do mvn -X clean install I don't get any error.Carrier
For those using Kotlin, you'll have to generate the Q-classes before compiling your sources: felixzett.com/articles/minimal-maven-kotlin-querydsl-exampleOhalloran
S
12

I have tested with your pom.xml. The Q classes were generated for me but I couldn't access them from my source code. The problem is that the generated-sources is not on classpath by default. Add that on the classpath and you will be able to use them in your source code.

  1. Check the target/generated-sources directory to see if the classes are actually there. (You should be able to find them because I tested with your pom.xml)
  2. If you add target/generated-sources to classpath, you application will work. But I don't think that is a good idea. Because all the files in the classpath will be indexed by the IDE and your IDE will be slower. All the files in the generated-sources folder need not be indexed. So add target/generated-sources/java to classpath and change your query-dsl plugin to generated Q class to target/generated-sources/java
Sectarianism answered 21/8, 2017 at 10:29 Comment(4)
Hey yes they are in target/generated-sources I will try your method and comment.Carrier
Would you mind explaining the class path stuff. Why exactly it didn't work earlier? That would be helpful. Thanks !!Carrier
The application should know that it has to look into that directory as well for the classes. You can see what classpath means in java. Modern IDEs like intellij add the generated-sources/java folder automatically to classpath.Sectarianism
Intellij by default excludes the /target from the classpath. that's why you can not access them. One option is to add /target as src folder from project settings. Other option is to configure maven plugin to generate code in the src folder at some package (preferably next to the entities package). And run "mvn clean compile" this should generate the qdsl classes.Heathenish
S
3

In my case, here, what was causing the error was the """ multiline string """ syntax from Java 14 (preview feature).

I figured it out by mvn clean compile -e.

Spiritualist answered 22/12, 2020 at 19:56 Comment(2)
After testing a lot of things on my IntelliJ, that command does the job! No more error for generated Q class!Businesswoman
Very similar issue here: we were using the Java 21 string template feature. Our root pom.xml had a correct configuration for the maven compiler plugin, but this same configuration needs to be added for the apt-maven-plugin of QueryDSL. See this Github issue comment for a workaround (i.e. adding <compilerOptions><some_flag_name>--enable-preview</some_flag_name></compilerOptions> to the plugin configuration in your pom.Rollmop
J
2

I recently have the same issue has you.

It seems apt-maven-plugin doesn't like empty .java file. If you have one in your project he will not generate any class without indication about the empty file, which can be very hard to find.

Joleen answered 1/12, 2020 at 15:17 Comment(0)
I
0

To solve the problem, i put the plugin com.mysema.maven before the plugin org.codehaus.mojo, this made the generated-source be put in the correct folder and was recognized by the intelijj. Example:

       ...

        <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>${basedir}/target/generated-sources</outputDirectory>
                        <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>${build.helper.maven.version}</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>target/generated-sources</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        ....

    </plugins>
</build>
Intendment answered 25/11, 2021 at 18:56 Comment(0)
N
0

Note: I am using IntelliJ

I was having trouble getting the files generated(the Q classes), and it turned out that building my maven project using IntelliJ would not work, so I had to run this command instead in the command line to build the project:

./mvnw install

And the files were generated for me, and in order to be able to use these generated files in my source code, I had to mark the folder that contains these files as "Generated Sources Root", so in this case navigate to this path:

target/generated-sources/java

right mouse click on java folder --> mark directory as --> Generated Sources Root.

After this you will be able to import these Q classes in your source code.

Noncompliance answered 12/11, 2022 at 13:45 Comment(0)
A
0

I faced the same problem in 2024 with QueryDSL version 5.1.0 and I solved it with the "classifier" field which contains "jakarta", like this:

<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-apt</artifactId>
    <version>${querydsl.version}</version>
    <classifier>jakarta</classifier>
    <scope>provided</scope>
</dependency>
Akkadian answered 31/7, 2024 at 13:16 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.