Cause
What happens is that because you designate it on the WrapperMapper it will not directly use the FruitMapper. Instead the WrapperMapper implementation will get a new mapping method generated to map fruits. MapStruct fails at this attempt and gives you that compile error.
Solution
If you add that ignore to the FruitMapper it would inherit it to both subclassmappings:
@SubclassMapping( source = Apple.class, target = AppleDto.class )
@SubclassMapping( source = Banana.class, target = BananaDto.class )
@Mapping( target = "weight", ignore = true )
FruitDto map( Fruit fruit );
In case you want to have a mapping with weights and one without weights you could add the @org.mapstruct.Named
annotation to the one without weights and then use this at the WrappedMapper.
like this:
@Mapper(subclassExhaustiveStrategy = SubclassExhaustiveStrategy.RUNTIME_EXCEPTION /*...*/)
public interface FruitMapper {
@SubclassMapping( source = Apple.class, target = AppleDto.class )
@SubclassMapping( source = Banana.class, target = BananaDto.class )
FruitDto map( Fruit fruit );
@SubclassMapping( source = Apple.class, target = AppleDto.class )
@SubclassMapping( source = Banana.class, target = BananaDto.class )
@Mapping( target = "weight", ignore = true )
@Named( "NoWeights" )
FruitDto map( Fruit fruit );
}
@Mapper(uses = {FruitMapper.class})
public interface WrapperMapper {
@Mapping( target = "fruit", qualifiedByName = "NoWeights" )
WrapperDto map(Wrapper wrapper)
}
CLICK HERE to find out more related problems solutions.