2
votes

I am getting a compile error:

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 

However when I remove the mapstruct annotation processor it compiles fine.

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

So I think that mapstruct is scanning the classes before they have been generated? Any solutions for this?

3
How are you adding the meta-model generator? Have you added both, the meta-model generator and MapStruct via annotationProcessorPaths? - Gunnar
Why does the annotationProcessorPath have to be manually added for mapstruct? 'If omitted, the default classpath is used to detect annotation processors.' Why doesn't the default classpath pick it up like with the JPA metamodelgen? - Peter Kipping
Both approaches should work: adding both processors to (optional) project dependencies or adding them via annotationProcessorPaths. I would recommend the latter. - Gunnar
I mean if you leave annotationProcessorPaths blank, shouldn't it scan all jars and pick up both the mapstruct and hibernate metamodel annontaion processors? - Peter Kipping
Yes, this should work, too. - Gunnar

3 Answers

2
votes

The problem is that the maven-compiler only picks up the MapStruct annotation processor and not the one from one that generates the Customer_ classes (I assume it is the Hibernate Metamodel Generator). Have a look at the documentation of annotationProcessorPaths.

You have 2 possibilities to fix your problem:

  1. Add all annotation processor the same way you are adding 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. Add MapStruct as provided dependency (in order not to be packaged with your jar/war) in your dependencies:

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

I would suggest to go with option 1, since that way you cannot accidentally use a transitive dependency from some of the annotation processors.

1
votes

I added the hibernate JPA modelgen jar to the path

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

Now it is working, thanks

0
votes

I was having the same issue. I ended up using the org.bsc.maven plugin and using the <includes>. You can also add a <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>