@Target(value=METHOD) @Retention(value=CLASS) public @interface InheritInverseConfiguration
Mapping
s from an inverse mapping method to the annotated method
as well. An inverse mapping method is a method which has the annotated method's source type as target type (return
type or indicated through a parameter annotated with MappingTarget
) and the annotated method's target type as
source type.
Any mappings given on the annotated method itself are added to those mappings inherited from the inverse method. In case of a conflict local mappings take precedence over inherited mappings.
If more than one matching inverse method exists, the name of the method to inherit the configuration from must be
specified via name()
Mapping.expression()
, Mapping.constant()
, Mapping.defaultExpression()
and
Mapping.defaultValue()
are not inverse inherited
Examples
@Mapper
public interface HumanMapper {
Human toHuman(HumanDto humanDto);
@InheritInverseConfiguration
HumanDto toHumanDto(Human human);
}
// generates
public class HumanMapperImpl implements HumanMapper {
@Override
public Human toHuman(HumanDto humanDto) {
if ( humanDto == null ) {
return null;
}
Human human = new Human();
human.setName( humanDto.getName() );
return human;
}
@Override
public HumanDto toHumanDto(Human human) {
if ( human == null ) {
return null;
}
HumanDto humanDto = new HumanDto();
humanDto.setName( human.getName() );
return humanDto;
}
}
@Mapper
public interface CarMapper {
@Mapping( target = "seatCount", source = "numberOfSeats")
@Mapping( target = "enginePower", source = "engineClass", ignore=true) // NOTE: source specified as well
CarDto carToDto(Car car);
@InheritInverseConfiguration
@Mapping(target = "numberOfSeats", ignore = true)
// no need to specify a mapping with ignore for "engineClass": specifying source above will assume
Car carDtoToCar(CarDto carDto);
}
public abstract String name
Copyright © 2012-2021 MapStruct Authors; All rights reserved. Released under the Apache Software License 2.0.