java - Querydsl maven编译错误: QClass.类不存在

标签 java spring maven querydsl

我是 Querydsl 的新手,并尝试在一个简单的测试项目中使用它。 我按照官方教程配置了我的pom.xml,然后mvn clean install能够在target/generated-sources/java下生成Q类。 但我收到以下错误:

[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

我认为根本原因是生成的Q类源文件没有自动编译成二进制类文件。我确实验证了我的项目目录下没有 QUser.class 。 我还尝试使用 build-helper-maven-plugin 添加 target/generated-sources/java 作为源文件夹,并在 apt-maven-plugin 配置中指定 target/generated-sources/java 作为附加源根目录。但我没有运气。

这是我的 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>

这是生成的 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;
    }

}

下面是 UserController.java 中对 QUser 的唯一引用。 repository 是 UserRepository 存储库的一个实例,它扩展了 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);

感谢您的帮助和评论。如果需要,我可以提供更多信息。

最佳答案

我再次从头开始构建一切,最终 Querydsl 开始工作了。 我认为这个问题是由于spring data jpa和querydsl的版本不兼容引起的。 现在我使用 spring data jpa 1.3.2.RELEASE 和 querydsl 2.8.0.,一切正常。 附:我删除了additionalSourceRoots 和build-helper-maven-plugin 配置。它们是不必要的。

关于java - Querydsl maven编译错误: QClass.类不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23040620/

相关文章:

java - JDepend 依赖约束失败

java - 没有注释的 jackson 是否绝对需要二传手?

java - SQLContext.sql 上的 Spark NoSuchMethodError(Cloudera 5.8.0 上的 Spark 1.6.0)

java - spring Controller 方法中相同类型的两个对象( session 和非 session )非 session 正在替换 session

java - 如何自定义SpringWebFlux WebClient xml+rss JAXB反序列化?

java - 将JSoup编译为Maven依赖关系的麻烦-Java

java - DialogListener 的 startActivity 导致应用程序崩溃

spring - 浏览器被重定向到 service-worker.js

java - 使用 PropertyPlaceholderConfigurer 时 ${project.name} 丢失

windows - 自定义 MSI 安装程序