Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity mapping to DTO with DI with MapStruct

I am new in mapstruct and I am using spring as DI I follow up MapStruct documentation page regarding DI containers section 4.2 I tried to map my entity to dto as below :

@Mapper(componentModel = "spring")
public interface CustomerMapper {
@Mapping(source = "registered",target = "activeProfile")
CustomerDto customerToCustomerDto(Customer customer);
}

when i run mvn install i got this error :

java:27: error: No property named "registered" exists in source parameter(s).
@Mapping(source = "registered",target = "activeProfile")

my entity using lombok and I am sure there is registered field

please help

like image 220
Ahmad Abdulaziz Avatar asked Dec 29 '15 20:12

Ahmad Abdulaziz


4 Answers

You don't have to to remove Lombok. You can setup it to work before MapStruct as was described here by ahus1 https://github.com/mapstruct/mapstruct/issues/510

<!-- first de-lombok the sources to make getters/setters visible for mapstruct,
but *DON'T'* add the output directory to the sources, as we will only need it for mapstruct processing -->
<plugin>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok-maven-plugin</artifactId>
    <version>${org.projectlombok.maven.version}</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>delombok</goal>
            </goals>
            <configuration>
                <sourceDirectory>src/main/java</sourceDirectory>
                <addOutputDirectory>false</addOutputDirectory>
                <outputDirectory>${project.build.directory}/delombok</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

<!-- use the de-lomobok files to create the necessary mappers -->
<plugin>
    <groupId>org.bsc.maven</groupId>
    <artifactId>maven-processor-plugin</artifactId>
    <version>2.2.4</version>
    <configuration>
        <defaultOutputDirectory>
            ${project.build.directory}/generated-sources/mapstruct
        </defaultOutputDirectory>
        <processors>
            <processor>org.mapstruct.ap.MappingProcessor</processor>
        </processors>
        <sourceDirectory>
            ${project.build.directory}/delombok
        </sourceDirectory>
    </configuration>
    <executions>
        <execution>
            <id>process</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>process</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<!-- now take the original sources together with the created mappers to the compiler -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.0</version>
    <configuration>
        <annotationProcessors>
            <annotationProcessor>lombok.launch.AnnotationProcessorHider$AnnotationProcessor</annotationProcessor>
        </annotationProcessors>
    </configuration>
</plugin>
like image 186
Pavel Bely Avatar answered Nov 18 '22 14:11

Pavel Bely


I've solved the problem using annotationProcessors in pom.xml

   <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.0</version>
        <configuration>
            <annotationProcessorPaths>
                <path>
                   <groupId>org.projectlombok</groupId>
                   <artifactId>lombok</artifactId>
                   <version>${lombok.version}</version>
                </path>
                <path>
                   <groupId>org.mapstruct</groupId>
                   <artifactId>mapstruct-processor</artifactId>
                   <version>${mapstruct.version}</version>
                </path>
            </annotationProcessorPaths>
        </configuration>
  </plugin>
like image 22
Yacine Avatar answered Nov 18 '22 16:11

Yacine


I removed lombok from Entity and created setters /getters manually and worked well

like image 2
Ahmad Abdulaziz Avatar answered Nov 18 '22 15:11

Ahmad Abdulaziz


Neither you have to remove lombok from your project nor you have to mess up the maven compiler plugin. You just have to declare the lombok dependency before you declare the mapstruck dependency in your pom. With this order maven is able to de-lombok your classes, before mapstruct referes to the getters and setters. This is possibly a feature of maven's dependency mediation.

Dependency mediation - this determines what version of a dependency will be used when multiple versions of an artifact are encountered. Currently, Maven 2.0 only supports using the "nearest definition" which means that it will use the version of the closest dependency to your project in the tree of dependencies. You can always guarantee a version by declaring it explicitly in your project's POM. Note that if two dependency versions are at the same depth in the dependency tree, until Maven 2.0.8 it was not defined which one would win, but since Maven 2.0.9 it's the order in the declaration that counts: the first declaration wins.

https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Transitive_Dependencies

like image 2
Patrick Lindner Avatar answered Nov 18 '22 14:11

Patrick Lindner