Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mapstruct: update existing field of entity using data from DTO

Tags:

mapstruct

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()

like image 988
yaroslavTir Avatar asked Jul 11 '17 14:07

yaroslavTir


People also ask

How do you map entity to DTO using MapStruct?

First, you need to add mapstruct and mapstruct-processor to your project's dependencies. The latter will be used by MapStruct to generate the mapper implementations during build-time. Besides a MapStruct dependency, a maven-compiler-plugin plugin must be also configured in your pom. xml file.

What is componentModel in MapStruct?

Enclosing class: MappingConstants public static final class MappingConstants.ComponentModel extends Object. Specifies the component model constants to which the generated mapper should adhere. It can be used with the annotation Mapper.componentModel() or MapperConfig.componentModel()

What is MappingTarget in MapStruct?

Annotation Type MappingTargetDeclares a parameter of a mapping method to be the target of the mapping. Not more than one parameter can be declared as MappingTarget . NOTE: The parameter passed as a mapping target must not be null .

Does MapStruct use reflection?

During compilation, MapStruct will generate an implementation of this interface. This implementation uses plain Java method invocations for mapping between source and target objects, i.e. no reflection or similar.


1 Answers

This is a known issue, see #1011. This has been improved in 1.2.0(at the time of writing 11.07.2017 the latest version is 1.2.0.Beta3). You should try the latest version, it should work as expected.

like image 129
Filip Avatar answered Oct 13 '22 18:10

Filip