mapstruct / mapstruct-examples

Examples for using MapStruct
Other
1.28k stars 511 forks source link

Mapping annotation field name is different from entity field name #122

Closed CurryPotato closed 3 years ago

CurryPotato commented 3 years ago

mapstruct version: 1.4.1.Final jdk version: 8

public class Driver {
    private String sourceName;
    private String pathName;
}

public class DriverDTO {
    private String szName;
    private String pName;
}

@Mapper
public interface DriverMapper {
    @Mapping(source = "sourceName", target = "szName")
    @Mapping(source = "parentName", target = "pName")
    DriverDTO driverToDriverDTO(Driver driver);
}

when I compile these class, I get a compile error.

Error:(26, 12) java: Unknown property "pName" in result type DriverDTO. Did you mean "PName"?

but I make target = "pName" -> target = "PName" , I get a correct result.

filiphr commented 3 years ago

This works as designed. MapStruct uses the Java bean mapping convention to detect the properties.

Your setters or most likely looking like setPName. When the second character is upper case we do not lowercase the first one. That's why the name of the property is PName

CurryPotato commented 3 years ago

javabean support uppercase abbreviation field like XMLName. so a javabean field like XXxxx and xxXxxx is right , and xXxxx and Xxxx is wrong . thank you!