3

I have a situation, where I need to map multi objects (in a flat structure) into one object (an hierarchy object) in Java using ModelMapper.

For example,

class Person{
    String name;
    int age;
}

class Address{
    int streetSumber;
    String streetName;
    String city;
}

class Phone{
    String type;
    String number;    
}

What I want to get out from the model mapper is something like

class PersonDTO{

    String name;
    int age;

    class AddressDTO{
        int streetSumber;
        String streetName;
        String city;
    }

    class PhoneDTO{
        String type;
        String number;    
    }
}

Is there a way to simple achieve this?

Thanks

jasonfungsing
  • 1,625
  • 8
  • 22
  • 34

1 Answers1

0

You must create a PersonWrapper

class PersonWrapper {
  Person person;
  Address address;
  Phone phone;
}

and map PersonWrapper to PersonDTO, for example

PropertyMap<PersonWrapper, PersonDTO> orderMap = new PropertyMap<Order, OrderDTO>() {
  protected void configure() {
    map().setName(source.getPerson().getName());
    ....
  }
};
jtomaszk
  • 9,223
  • 2
  • 28
  • 40