2017-01-12 6 views
0

ich einen Compiler-Fehler bekommen:Funktioniert Mapstruct mit JPA-Metamodellklassen?

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 

Allerdings, wenn ich die mapstruct Annotation Prozessor entfernen kompiliert es in Ordnung.

Also ich denke, dass Mapstruct die Klassen scannt, bevor sie generiert wurden? Irgendwelche Lösungen dafür?

+0

Wie Sie den Meta-Modell-Generator hinzugefügt werden? Haben Sie den Metamodellgenerator und MapStruct über 'annotationProcessorPaths' hinzugefügt? – Gunnar

+0

Warum muss der annotationProcessorPath manuell für mapstruct hinzugefügt werden? 'Falls nicht angegeben, wird der Standardklassenpfad zur Erkennung von Annotationsprozessoren verwendet.' Warum greift der Standardklassenpfad nicht wie beim JPA Metamodell auf? –

+0

Beide Ansätze sollten funktionieren: Hinzufügen von Prozessoren zu (optionalen) Projektabhängigkeiten oder Hinzufügen von ihnen über AnnotationProcessorPaths. Ich würde das letztere empfehlen. – Gunnar

Antwort

0

ich hinzugefügt, um das JPA modelgen jar Hibernate auf den Weg

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

Jetzt funktioniert es dank

1

Das Problem ist, dass die maven-compiler nur die MapStruct Annotation Prozessor und nicht die von einem, der die Customer_ Klassen generiert (ich nehme an, es ist der Hibernate Metamodell Generator). Schauen Sie sich die Dokumentation annotationProcessorPaths an.

Sie haben zwei Möglichkeiten, das Problem zu beheben:

  1. hinzufügen alle Annotations-Prozessor die gleiche Art und Weise Sie hinzufügen 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. hinzufügen MapStruct als vorgesehen Abhängigkeit (in der Reihenfolge nicht mit Ihrem Glas/Krieg verpackt werden) in Ihren Abhängigkeiten:

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

Ich würde vorschlagen, mit Option 1 zu gehen, da Sie auf diese Weise nicht versehentlich eine transitive Abhängigkeit von einigen der Annotationsprozessoren verwenden können.

0

Ich hatte das gleiche Problem. Ich habe das org.bsc.maven Plugin verwendet und die <includes> verwendet. Sie können auch eine <defaultOutputDirectory> hinzufügen.

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