17

I have 2 entities:

Entity 1:

public class Master {

    private int id;
    private Set<SubMaster> subMasters= new HashSet<SubMaster>(0);
}

public class SubMaster{
    private int subId;
    private String subName;
}

Entity 2:

public class MasterDTO {

    private int id;
    private Set<SubMaster> subMasters= new HashSet<SubMaster>(0);
}

public class SubMasterDTO{
    private int subId;
    private String subName;
}

I am using MapStruct Mapper to map values of POJO to another.

public interface MasterMapper{
    MasterDTO toDto(Master entity);
}

I am able to successfully map Master to MasterDTO. But, the nested collection of SubMaster in Master is not getting mapped to its counterpart in MasterDTO.

Could anyone help me in right direction?

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
gschambial
  • 1,383
  • 2
  • 11
  • 22
  • Are you using version `1.2.0`? If yes then MapStruct should automatically create everything for you. Also you are missing `@Mapper` on your interface. Also your set in the `MasterDTO` is `SubMaster` and not `SubMasterDTO` (I am not sure if you have it like that in your code as well). – Filip Aug 22 '17 at 14:57

1 Answers1

30

This example in Mapstruct's Github repo is an exact showcase for what you're trying to do.

TL;DR You'll need a separate mapper for the SubMaster (let's call it SubMasterMapper) class and then put a @Mapper(uses = { SubMasterMapper.class }) annotation on your MasterMapper:

public interface SubMasterMapper {
    SubMasterDTO toDto(SubMaster entity);
}

@Mapper(uses = { SubMasterMapper.class })
public interface MasterMapper {
    MasterDTO toDto(Master entity);
}
jannis
  • 4,843
  • 1
  • 23
  • 53