VB_
VB_

Reputation: 45682

MapStruct do not map some attributes

Mapstruct throws me the next error on compilation stage:

ConfigsMapperImpl is not abstract and does not override abstract method toConfigs(ConfigsDTO)

At the same moment MapStruct generate code for all other methods well.

I suppose that happens because Config object has more fields than ConfigsDTO.

@Mapper
public interface ConfigsMapper {

    ConfigsMapper INSTANCE = Mappers.getMapper(ConfigsMapper.class);

    ConfigsDTO ConfigsToConfigsDTO(Configs configs);

    List<ConfigsDTO> toConfigsDTOs(List<Configs> configs);

    @InheritInverseConfiguration
    Configs toConfigs(ConfigsDTO configsDTO);
}

Upvotes: 1

Views: 9770

Answers (1)

rodridevops
rodridevops

Reputation: 1987

In the generated method implementations all readable properties from the source type (e.g. Configs) will be copied into the corresponding property in the target type (e.g. ConfigsDTO). If a property has a different name in the target entity, its name can be specified via the @Mapping annotation.

The annotation @Mappings define which attributes from source will be transferred to specific attribute in target. The annotation define that @InheritInverseConfiguration inverse mapping to be done.

For example:

@Mapper
public interface ConfigsMapper {
    ConfigsMapper INSTANCE = Mappers.getMapper(ConfigsMapper.class);

    @Mappings({ 
        @Mapping(source = "configs1", target = "configsDTO1"),
        @Mapping(source = "configs2", target = "configsDTO2"),
        @Mapping(target = "somethingElse", constant="somethingElseOnDTO")
    })
    ConfigsDTO ConfigsToConfigsDTO(Configs configs);

    @InheritInverseConfiguration
    Configs toConfigs(ConfigsDTO configsDTO);
}

Upvotes: 5

Related Questions