MapStruct : Mapping a object property to an upper level object

863 views Asked by At

Consider the below classes

class ContributionDoc {
  private String contributorName;
  private String version;
  private int contributionNumber;
  private List<Contribution> contributions;
}
class Contribution {
  private String metadata;
}

Class ContributionDoc needs to be mapped to class ContributionDocDto.
Class Contribution needs to be mapped to class ContributionDetails.

class ContributionEntity {
  private String contributorName;
  private int contributionNumber;
}

Both class ContributionDocDto and ContributionDetails have ContributionEntity (because we later realized that ContributionEntity needs to be moved from ContributionDocDto to ContributionDetails)

class ContributionDocDto {
  private ContributionEntity;
  private String docVersion;
  private List<Contribution> contributions;
}
class ContributionDetails {
  private ContributionEntity;
  private String metadata;
}

I have below mappers with usual annotations from mapstruct

ContributionDocDto map (ContributionDoc doc)
ContributionDetails map (Contribution contrib)

How can I populate an instance of ContributionEntity inside ContributionDetails ?

0

There are 0 answers