2

我收到编译错误:

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 在生成类之前正在扫描它们?有什么解决方案吗?

4

3 回答 3

2

问题是maven-compiler唯一选择 MapStruct 注释处理器,而不是生成Customer_类的一个(我假设它是 Hibernate Metamodel Generator)。查看annotationProcessorPaths的文档。

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

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

于 2017-01-13T16:41:22.407 回答
1

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

现在可以用了,谢谢

于 2017-01-22T23:39:37.270 回答
0

我遇到了同样的问题。我最终使用了 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>
于 2017-01-20T23:00:43.060 回答