7

Using MapStruct. Take as example:

class Dto {
    DtoA a;
    DtoB b;
}

class DtoA {
    Long id;
    //...
}

class DtoB {
    Long id;
    //...
}


class Entity {
    AB ab;
}

How do I map DtoA and DtoB to AB ?

I tried:

public abstract Entity toEntity(Dto dto);

@Mappings({
            @Mapping(source = "a", target = "ab.a"),
            @Mapping(source = "b", target = "ab.b")
)}
public abstract AB toABEntity(DtoA a, DtoB b);

Altough code has been generated*, the method toABEntity is not invoked.

*Badly, as it sets first a but then to set b creates a new instance of ab, so a is lost.

5
  • Which version of MapStruct is this?
    – Filip
    Commented Nov 5, 2018 at 11:14
  • @Filip 1.2.0Final
    – anat0lius
    Commented Nov 5, 2018 at 11:15
  • What is AB in line public abstract AB toABEntity(DtoA a, DtoB b);?
    – jannis
    Commented Nov 5, 2018 at 11:35
  • @jannis I'm doing only one call: toEntity, as I want to map Dto to Entity. I've defined toABEntity hoping MapStruct will use it within toEntity.
    – anat0lius
    Commented Nov 5, 2018 at 11:38
  • Please describe exactly what do you want to map and where. It would be best if you give the desired input and output (an example Dto and example resulting Entity).
    – jannis
    Commented Nov 5, 2018 at 11:48

1 Answer 1

3

As far as I understand You want to map Entity to Dto and combine two fields Dto.a and Dto.b to a single field Entity.ab.

Normally when you try to do this like this:

@Mapper
public interface TestMapper {
    @Mappings({
        @Mapping(source = "a.id", target = "ab.aId", qualifiedByName = "toAB"),
        @Mapping(source = "b.id", target = "ab.bId", qualifiedByName = "toAB"),
    })
    Entity toEntity(Dto dto);
}

The generated mapper overides ab instance for every @Mapping having a target property within ab. It's clearly a bug and there's a ticket for this at MapStructs GitHub: https://github.com/mapstruct/mapstruct/issues/1148

There's a workaround though:

@Mapper
public interface TestMapper {
    @Mappings({
        @Mapping(source = "dto", target = "ab", qualifiedByName = "toAB"),
    })
    Entity toEntity(Dto dto);

    @Mappings({
        @Mapping(target = "aId", source = "a.id"),
        @Mapping(target = "bId", source = "b.id"),
    })
    AB toAB(Dto dto);
}

I assumed the AB class to be:

class AB {
    public Long aId;
    public Long bId;
}

Generated mapper code:

public class TestMapperImpl implements TestMapper {

    @Override
    public Entity toEntity(Dto dto) {
        if ( dto == null ) {
            return null;
        }

        Entity entity = new Entity();

        entity.ab = toAB( dto );

        return entity;
    }

    @Override
    public AB toAB(Dto dto) {
        if ( dto == null ) {
            return null;
        }

        AB aB = new AB();

        Long id = dtoBId( dto );
        if ( id != null ) {
            aB.bId = id;
        }
        Long id1 = dtoAId( dto );
        if ( id1 != null ) {
            aB.aId = id1;
        }

        return aB;
    }

    private Long dtoBId(Dto dto) {
        if ( dto == null ) {
            return null;
        }
        DtoB b = dto.b;
        if ( b == null ) {
            return null;
        }
        Long id = b.id;
        if ( id == null ) {
            return null;
        }
        return id;
    }

    private Long dtoAId(Dto dto) {
        if ( dto == null ) {
            return null;
        }
        DtoA a = dto.a;
        if ( a == null ) {
            return null;
        }
        Long id = a.id;
        if ( id == null ) {
            return null;
        }
        return id;
    }
}
1

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.