映射结构是否适用于 JPA 元模型类



我收到编译错误:

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 

但是,当我删除映射结构注释处理器时,它可以很好地编译。

            <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 元模型生成器)。查看 annotationProcessorPath 的文档。

您有两种方法可以解决问题:

    添加
  1. 所有注释处理器的方式与添加 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,因为这样您就不会意外使用来自某些注释处理器的传递依赖项。

我将休眠 JPA 模型生成 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>

现在它正在工作,谢谢

我遇到了同样的问题。我最终使用了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>

最新更新