mapstruct 是否适用于 JPA 元模型 类?

Does mapstruct work with JPA meta model classes?

我遇到编译错误:

com/mycompany/hibernate5/Main.java:[10,46] cannot find symbol
  symbol:   class Customer_
  location: package com.mycompany.hibernate5.sakila.domain
com/mycompany/hibernate5/Main.java:[11,46] cannot find symbol
  symbol:   class Address_
  location: package com.mycompany.hibernate5.sakila.domain
2 errors 

然而,当我删除 mapstruct 注释处理器时,它可以正常编译。

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
<!--                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>-->
            </plugin>

所以我认为 mapstruct 在 类 生成之前扫描它们?对此有什么解决方案吗?

问题是 maven-compiler 只获取 MapStruct 注释处理器,而不是生成 Customer_ 类 的注释处理器(我假设它是 Hibernate 元模型生成器).查看 annotationProcessorPaths.

的文档

您有 2 种可能性来解决您的问题:

  1. 添加 all 注释处理器,与添加 MapStruct 的方式相同:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
            <annotationProcessorPaths>
                <!-- Here you add the other paths -->
                <path>
                    <groupId>org.mapstruct</groupId>
                    <artifactId>mapstruct-processor</artifactId>
                    <version>${org.mapstruct.version}</version>
                </path>
            </annotationProcessorPaths>
        </configuration>
    </plugin>
    
  2. 在你的依赖项中添加 MapStruct 作为提供的依赖项(为了不与你的 jar/war 一起打包):

    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct-processor</artifactId>
        <version>${org.mapstruct.version}</version>
        <scope>provided</scope>
    </dependency>
    

我建议选择选项 1,因为这样你就不会意外地使用来自某些注释处理器的传递依赖。

我遇到了同样的问题。我最终使用了 org.bsc.maven 插件并使用了 <includes>。您还可以添加 <defaultOutputDirectory>.

<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
        <includes>**/mapper/*.java</includes> <!--package where annotated mapper classes reside-->
        <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>${org.mapstruct.version}</version>
        </dependency>
    </dependencies>
</plugin>
<plugin>
    <groupId>org.apache.openjpa</groupId>
    <artifactId>openjpa-maven-plugin</artifactId>
    <configuration>
        <includes>**/persistence/*.class</includes>
        <excludes>**/persistence/*_.class</excludes>
        <addDefaultConstructor>true</addDefaultConstructor>
        <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
    </configuration>
    <executions>
        <execution>
            <id>enhancer</id>
            <phase>process-classes</phase>
            <goals>
                <goal>enhance</goal>
            </goals>
        </execution>
    </executions>
</plugin>

我将 hibernate JPA modelgen jar 添加到路径

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.mapstruct</groupId>
                        <artifactId>mapstruct-processor</artifactId>
                        <version>${org.mapstruct.version}</version>
                    </path>
                    <path>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-jpamodelgen</artifactId>
                        <version>5.2.6.Final</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>

现在可以使用了,谢谢