【问题标题】:Does mapstruct work with JPA meta model classes?mapstruct 是否适用于 JPA 元模型类?
【发布时间】:2017-01-23 07:39:37
【问题描述】:

我收到一个编译错误:

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 是在生成类之前对其进行扫描?有什么解决办法吗?

【问题讨论】:

  • 如何添加元模型生成器?您是否通过annotationProcessorPaths 添加了元模型生成器和 MapStruct?
  • 为什么mapstruct要手动添加annotationProcessorPath? “如果省略,则使用默认类路径来检测注释处理器。”为什么默认类路径不像 JPA metamodelgen 那样选择它?
  • 两种方法都应该有效:将两个处理器添加到(可选)项目依赖项或通过 annotationProcessorPaths 添加它们。我会推荐后者。
  • 我的意思是,如果您将 annotationProcessorPaths 留空,它不应该扫描所有 jar 并拾取 mapstruct 和 hibernate 元模型 annontaion 处理器吗?
  • 是的,这也应该可以。

标签: mapstruct


【解决方案1】:

问题在于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,因为这样您就不会意外使用来自某些注释处理器的传递依赖。

【讨论】:

    【解决方案2】:

    我将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>
    

    现在可以了,谢谢

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题。我最终使用了 org.bsc.maven 插件并使用了&lt;includes&gt;。您也可以添加&lt;defaultOutputDirectory&gt;

      <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>
      

      【讨论】:

        猜你喜欢
        • 2023-03-25
        • 1970-01-01
        • 2020-05-26
        • 1970-01-01
        • 1970-01-01
        • 2017-07-18
        • 1970-01-01
        • 2010-11-30
        • 1970-01-01
        相关资源
        最近更新 更多