Querydsl maven compilation error: QClass.class does not exist

14,278

Solution 1

I built everything from scratch again and finally Querydsl is working. I think this problem was caused by incompatible versions of spring data jpa and querydsl. Now I am using spring data jpa 1.3.2.RELEASE and querydsl 2.8.0., everything works perfectly. P.S. I removed the additionalSourceRoots and the build-helper-maven-plugin config. They are unnecessary.

Solution 2

It work for me like this

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.10</version>
            <executions>
                <execution>
                    <id>add-resource</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/main/sources</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <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>src/main/sources</outputDirectory>
                        <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.mysema.querydsl</groupId>
                    <artifactId>querydsl-apt</artifactId>
                    <version>3.4.3</version>
                </dependency>
            </dependencies>
        </plugin>
Share:
14,278
Frey Zheng
Author by

Frey Zheng

Updated on July 28, 2022

Comments

  • Frey Zheng
    Frey Zheng over 1 year

    I am new to Querydsl and trying to use it in a simple test project. I followed the official tutorial to configure my pom.xml, then mvn clean install was able to generated the Q Classes under target/generated-sources/java. But I got the error below:

    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project spring-jqgrid-tutorial: Compilation failure
    
    [ERROR] /D:/Project/spring-jqgrid-tutorial/src/main/java/org/krams/controller/UserController.java:[92,62] package QUser.user does not exist
    

    I think the root cause is the generated Q class source files were not compiled automatically into binary class files. I did verify there are no QUser.class under my project directory. I also tried to use build-helper-maven-plugin to add target/generated-sources/java as a source folder and specify target/generated-sources/java as an additional source root in apt-maven-plugin configuration. But I got no luck.

    Here is my pom.xml

    <properties>
        <querydsl.version>3.3.2</querydsl.version>
        <maven.compiler.plugin.version>3.1</maven.compiler.plugin.version>
        <maven.apt.plugin.version>1.1.1</maven.apt.plugin.version
        <maven.build.helper.plugin.version>1.8</maven.build.helper.plugin.version>
    <properties>
    
    <dependencies>
        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
            <version>${querydsl.version}</version>
            </dependency>
        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <version>${querydsl.version}</version>
            <scope>provided</scope>
        </dependency>
     <dependencies>
    
    <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven.compiler.plugin.version}</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
    
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                    <version>${maven.build.helper.plugin.version}</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>target/generated-sources/java</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    
            <plugin>
                <groupId>com.mysema.maven</groupId>
                <artifactId>apt-maven-plugin</artifactId>
                <version>${maven.apt.plugin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/generated-sources/java</outputDirectory>
                            <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
                            <additionalSourceRoots>
                                <additionalSourceRoot>target/generated-sources/java</additionalSourceRoot>
                            </additionalSourceRoots>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    <plugins>
    

    And here is the generated QUser.java

    package org.krams.domain;
    
    import static com.mysema.query.types.PathMetadataFactory.*;
    
    import com.mysema.query.types.path.*;
    
    import com.mysema.query.types.PathMetadata;
    import javax.annotation.Generated;
    import com.mysema.query.types.Path;
    import com.mysema.query.types.path.PathInits;
    
    
    /**
     * QUser is a Querydsl query type for User
     */
    @Generated("com.mysema.query.codegen.EntitySerializer")
    public class QUser extends EntityPathBase<User> {
    
        private static final long serialVersionUID = -1712499619L;
    
        private static final PathInits INITS = PathInits.DIRECT2;
    
        public static final QUser user = new QUser("user");
    
        public final NumberPath<Integer> age = createNumber("age", Integer.class);
    
        public final StringPath firstName = createString("firstName");
    
        public final NumberPath<Long> id = createNumber("id", Long.class);
    
        public final StringPath lastName = createString("lastName");
    
        public final StringPath password = createString("password");
    
        public final QRole role;
    
        public final StringPath username = createString("username");
    
        public QUser(String variable) {
            this(User.class, forVariable(variable), INITS);
        }
    
        public QUser(Path<? extends User> path) {
            this(path.getType(), path.getMetadata(), path.getMetadata().isRoot() ? INITS : PathInits.DEFAULT);
        }
    
        public QUser(PathMetadata<?> metadata) {
            this(metadata, metadata.isRoot() ? INITS : PathInits.DEFAULT);
        }
    
        public QUser(PathMetadata<?> metadata, PathInits inits) {
            this(User.class, metadata, inits);
        }
    
        public QUser(Class<? extends User> type, PathMetadata<?> metadata, PathInits inits) {
            super(type, metadata, inits);
            this.role = inits.isInitialized("role") ? new QRole(forProperty("role"), inits.get("role")) : null;
        }
    
    }
    

    Below is the only reference to QUser in UserController.java. repository is an instance of UserRepository repository, which extends QueryDslPredicateExecutor.

    package org.krams.controller;
    
    import java.util.List;
    
    import org.krams.domain.QUser;
    import org.krams.domain.Role;
    import org.krams.domain.User;
    import org.krams.repository.UserRepository;
    import org.krams.response.JqgridResponse;
    import org.krams.response.StatusResponse;
    import org.krams.response.UserDto;
    import org.krams.service.UserService;
    import org.krams.util.JqgridFilter;
    import org.krams.util.JqgridObjectMapper;
    import org.krams.util.UserMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.domain.Page;
    import org.springframework.data.domain.PageRequest;
    import org.springframework.data.domain.Pageable;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    ...
    
    if (qUsername != null) 
                users = repository.findAll(QUser.user.username.like(qUsername), pageRequest);
    

    Appreciate your help and comments. I can provide more information if required.

  • Frey Zheng
    Frey Zheng about 10 years
    Update spring-core, spring-data-jpa, querydsl to newest versions. All works fine! spring-core: 4.0.3.RELEASE spring-data-jpa: 1.5.1.RELEASE querydsl: 3.3.2 My previous spring-data-jpa version must be too low.
  • David
    David over 9 years
    Interesting - what does ${version.com.mysema.maven.apt-maven-plugin} resolve to in your configuration?