Nested Mapping in Mapstruct

AdamIJK picture AdamIJK · Aug 22, 2016 · Viewed 21.8k times · Source

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 my target class, the other is EDPurchaseOrder class which known as source file, here don't worry about the naming conventions I used, just 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;
    }

Answer

Gunnar picture Gunnar · Aug 23, 2016

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.