16

I am new to MapStruct API, can anyone say how to do nested Mapping? I have two classes one is my actual PurchaseOrder class, which is known as my target class, and the other is EDPurchaseOrder class which is known as the source file, Don't worry about the naming conventions I used, go with source and target files.

Source Classes:
Source class EDCustomerOrder and its reference classes:

    public class EDCustomerOrder{
        private Integer orderID;
        private String orderNumber;
        private BigDecimal orderTotalQty;
        private String UOM;
        private PickupDTO pickupPoints;
        private Integer supplierID;
        private String supplierName;
        private String supplierNature;
        private EDAddress supplierEDAddress;
    }

    public class EDPickup{
        private List<EDPOItem> items;
    }

    public class EDAddress{
        private String addressLine1;
        private String addressLine2;
        private String addressLine3;
        private String city;
        private String state;
        private string countryCode;
        private String country;
        private String postalCode;
    }

    public class EDPOItem{
        private Integer itemID;
        private String itemCode;
        private String itemDescription;
        private Integer itemQuantity;
    }

Target classes:
Here my target class CustomerOrder and its reference classes:

    public class CustomerOrder{
        private Integer orderID;
        private String orderNumber;
        private List<Pickup> pickupPoints;
        private Supplier supplierDetail;
    }

    public class Pickup{
        private Integer pickupID;
        private Integer pickupLocationNumber;
        private List<POItem> items;
    }

    public class POItem{
        private Integer itemID;
        private String itemCode;
        private String itemDescription;
        private Integer itemQuantity;
    }

    public class Supplier{
        private Integer supplierID;
        private String supplierName;
        private String supplierNature;
        private Address supplierAddress;
    }

    public class Address{
        private String addressLine1;
        private String addressLine2;
        private String addressLine3;
        private String city;
        private String state;
        private string countryCode;
        private String country;
        private String postalCode;
    }
Saikat
  • 14,222
  • 20
  • 104
  • 125
AdamIJK
  • 615
  • 2
  • 12
  • 22

2 Answers2

19

So I suppose you have the same hierarchy of objects on the target side, e.g. a SongDTO, LibraryDTO and TrackDTO.

Then you'd have to declare a mapping method for each of those pairs of corresponding objects, configuring it via @Mapping as needed:

public interface MyMapper {

    @Mapping(source="name", target="title")
    SongDTO songToDto(Song song);

    LibraryDTO libraryToDto(Library library);

    TrackDTO trackToDto(Track track);
}

Then e.g. the generated implementation of songToDto() will invoke libraryToDto() in order to map the song's library into the song DTO's library DTO.

Also check out the reference guide to learn more.

Gunnar
  • 18,095
  • 1
  • 53
  • 73
  • Hi @Gunnar Thanks for your reply, I refined my question can u help with that again to me – AdamIJK Aug 23 '16 at 11:47
  • Hi @Gunnar I tried your suggestions and I'm following the same like you given above its working for me, but I'm trying some different scenarios in that facing difficulties to map my classes can u give your mail Id? I will send my all my questions. – AdamIJK Aug 24 '16 at 14:17
  • 2
    Please rather post your questions here on StackOverflow, so others can learn from the answers, too. But I recommend to check out the reference guide, as it describes all sorts of mapping options in great length. So chances are you'll find your answers there. Thanks. – Gunnar Aug 24 '16 at 14:34
  • as per your advice I added my complete scenario kindly help me. Thanks in advance – AdamIJK Sep 21 '16 at 07:26
  • 3
    This answer is hard to understand with its example. The class names in the question does not fit the ones in the answer. There is no songDto or trackToDto in the question(anymore?). Can this be edited? – qweret Jan 14 '22 at 07:07
-1
@Mapper(componentModel = "spring",unmappedTargetPolicy = ReportingPolicy.IGNORE)
    public interface CandidateProfessionalEntityAndDTOMapper {
        @Mappings({
            @Mapping(source = "company.companyId", target = "companyId"),
        })
        Clazz1
            entityToReferencesMapping(Clazz2 entity);
    }
    public class Clazz2 {
        private String companyName;
        private Company company;
    }
    public class Company{
        Integer companyId;
    }
    public class Clazz1 {
       private String companyId;
       private String companyName;
    }
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Miral Modi
  • 17
  • 2
  • 7
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Tyler2P Dec 28 '20 at 13:08