I recently added mapStruct in my project. This framework is cool, but I can not figure out one thing.
This is my case: I have Profile
entity and field with the Person
type. I want to update it using ProfileDto
. I am using void fromDto(ProfileDto dto, @MappingTarget Profile entity)
method for this. The problem is that mapper always create new Person instead of using person from profile
entity
My entity is:
public class Profile {
private Person person;
.. setters, getters and constructors
}
public class Person extends AbstractEntity {
private String name;
private String surname;
.. setters, getters and constructors
}
Dto
public class ProfileDto extends AbstractDto {
private String name;
private String surname;
.. setters, getters and constructors
}
my mapper
public abstract class ProfileMapper {
@Mappings({
@Mapping(target = "name", source = "entity.person.name"),
@Mapping(target = "surname", source = "entity.person.surname")
})
public abstract ProfileDto toDto(Profile entity);
@InheritInverseConfiguration(name = "toDto")
public abstract void fromDto(ProfileDto dto, @MappingTarget Profile entity);
}
generated code
@Override
public void fromDto(ProfileDto dto, Profile entity) {
if ( dto == null ) {
return;
}
Person person = new Person();
entity.setPerson( person );
...
I don't need to create new instance of person here
person = new Person();
I what somehow to replace this string with:
person = entity.getPerson()