2

I have below Source and Target classes, i am using lombok for generating getters and setters

public class Target {
    private String name;
    private String newName;
}

public class Source {
    private String name;
}

and let say if I want to map Source.name to Target.newName I am using below Mapper class with @Mapping to specify source and target variables.

but once i compile the code and check the generated ClassMapperImpl it is maping Source.name to Target.name and not to Target.new Name

@Mapper
public interface ClassMapper {

    @Mapping(source = "name", target = "newName")
    Target sourceToTarget(Source s);
}

1 Answer 1

2

I think they are both mapped when I try:

public class ClassMapperImpl implements ClassMapper {

    @Override
    public Target sourceToTarget(Source s) {
        if ( s == null ) {
            return null;
        }

        Target target = new Target();

        target.setNewName( s.getName() );
        target.setName( s.getName() );

        return target;
    }
}

Please use ignore on the name property.

@Mapper
public interface ClassMapper {

    @Mapping(source = "name", target = "newName")
    @Mapping(ignore = true, target = "name")
    Target sourceToTarget(Source s);
}
Sign up to request clarification or add additional context in comments.

3 Comments

this is a bug, right? We should create an issue I think.
Not sure. We always said we would key on target (not source). It is (deliberately) possible to map the same source to multiple targets with defined mappings. One could argue this should not happen for name based mappings tough..
Nope you are right. I didn't read that well. Even for name baaed it should always try to do the property name mapping for all non mapped target properties

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.