I am following the following document for implementing a mapper interface: MapStruct- Baeldung for Employee and EmployeeDTO.
Here is my Employee Class:
public class Employee {
private int id;
private String name;
// getters and setters
}
Here is my EmployeeDTO class:
public class EmployeeDTO {
private int employeeId;
private String employeeName;
// getters and setters
}
Here is my Mapper code:
@Mapper
public interface EmployeeMapper {
@Mapping(target="employeeId", source="entity.id")
@Mapping(target="employeeName", source="entity.name")
EmployeeDTO employeeToEmployeeDTO(Employee entity);
@Mapping(target="id", source="dto.employeeId")
@Mapping(target="name", source="dto.employeeName")
Employee employeeDTOtoEmployee(EmployeeDTO dto);
}
I am getting "Can't find related attribute" by hovering over entity.id. entity.name, dto.employeeId and dto.employeeName.
Why is it so?. It is working fine if I am removing the dot operation on entity and dto and just writing the fields name.
CodePudding user response:
I replicated the same it is working fine for me with dot operator. Maybe try adding the annotationProcessorPaths section to the configuration part of the maven-compiler-plugin plugin present in the baeldung document
This plugin given in the document:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.2.Final</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
Try if it helps
CodePudding user response:
put @Entity above Employee and EmployeeDTO class
CodePudding user response:
don't forget to use @Data on the EmployeeDTO class and @Entity on Employee class