Spring 数据 Jpa 查询 DSL Q 实体类未生成

Spring Data Jpa Query DSL Q Entityclasses not generating

这是 pom.xml 的要点。 我使用的查询 dsl 版本是 3.6.2

    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-core</artifactId>
    </dependency>
    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-apt</artifactId>
    </dependency>
    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-jpa</artifactId>
    </dependency>
</dependencies>
<build>
<plugins>
    <plugin>
        <groupId>org.bsc.maven</groupId>
        <artifactId>maven-processor-plugin</artifactId>
        <version>2.2.4</version>
        <configuration>
            <defaultOutputDirectory>
                ${project.build.directory}/generated-sources
            </defaultOutputDirectory>
            <processors>
                <processor>org.mapstruct.ap.MappingProcessor</processor>
            </processors>
        </configuration>
        <executions>
            <execution>
                <id>process</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>process</goal>
                </goals>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>${mapstruct.version}</version>
            </dependency>
        </dependencies>
    </plugin>
    <plugin>
        <groupId>com.mysema.maven</groupId>
        <artifactId>maven-apt-plugin</artifactId>
        <version>1.0.2</version>
        <executions>
            <execution>
                <phase>generate-sources</phase>
                <goals>
                    <goal>process</goal>
                </goals>
                <configuration>
                    <!-- Specifies the directory in which the query types are generated -->
                    <outputDirectory>target/generated-sources/querydsl</outputDirectory>
                    <!-- States that the APT code generator should look for JPA annotations -->
                    <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>
</build>

每当我 运行 mvn clean install 时,都会出现此异常消息

[ERROR] diagnostic: C:\Users\users\app\app-ims\app-ims-core\src\main\java\org\app\genesis\inventory\repo\ProductRepoImpl.java:6: error: cannot find symbol
import org.brightworks.genesis.inventory.model.QProduct;
                                              ^
  symbol:   class QProduct
  location: package org.app.genesis.inventory.model

这是 class 的要点。

import com.mysema.query.jpa.impl.JPAQuery;
import com.mysema.query.types.ConstructorExpression;
import org.brightworks.genesis.inventory.dto.ProductDTO;
import org.brightworks.genesis.inventory.model.QProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import javax.persistence.EntityManager;
import java.util.List;

    /**
     * 
     */
    public class ProductRepoImpl implements ProductRepoCustom {

        @Autowired
        private EntityManager em;

        @Override
        public Page<ProductDTO> search(String term, Pageable pageable) {
            JPAQuery query = new JPAQuery(em);
            QProduct product = QProduct.product;

模型和 repo 在同一个项目中,但是没有生成 Q 实体 classes。我该如何解决这个问题?

您的 mvn apt 插件之间存在冲突。你有两个,你只需要一个。只需相应地更改它们即可。这是一个例子。

    <plugin>
        <groupId>org.bsc.maven</groupId>
        <artifactId>maven-processor-plugin</artifactId>
        <version>2.2.4</version>
        <configuration>
            <defaultOutputDirectory>
                ${project.build.directory}/generated-sources
            </defaultOutputDirectory>
            <processors>
                <processor>org.mapstruct.ap.MappingProcessor</processor>
                <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
            </processors>
        </configuration>
        <executions>
            <execution>
                <id>process</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>process</goal>
                </goals>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>org.mapstruct</groupId>
                <artifactId>mapstruct-processor</artifactId>
                <version>${mapstruct.version}</version>
            </dependency>
        </dependencies>
    </plugin>

如果您愿意,也可以反过来使用 apt-maven-plugin,配置非常相似:

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