0

Entity Classes:

class User{
    private Name name;
    private int age;
    private String email;
    private Date dob;
    private Address address;

  // No Arguments Constructor , All Arguments Constructor , Setters, Getters and toString
}


class Name {
    private String firstName;
    private String lastName;

   // No Arguments Constructor , All Arguments Constructor , Setters, Getters and toString
}

class Address {
    private String houseNo;
    private String street;
    private String city;
    private Integer pincode;

    // No Arguments Constructor , All Arguments Constructor , Setters, Getters and toString
}

DTO:

class UserDTO{
    private String firstName;
    private String lastName;
    private int age;
    private String email;
    private Date dob;
    private String houseNo;
    private String street;
    private String city;
    private Integer pincode;

   // No Arguments Constructor , All Arguments Constructor , Setters, Getters and toString
 }

Code to convert Entity to DTO:

public class ReferenceTypePropertiesMapper {

@Test
public void shouldPopulateAllSimpleProperties(){
    User user = createUser();
    ModelMapper modelMapper = new ModelMapper();
    UserDTO userDTO = modelMapper.map(user,UserDTO.class);

    System.out.println("Source : "+ user);
    System.out.println("Destination : "+ userDTO);
}

private User createUser(){
    Name name = new Name("Siva", "Prasad");
    Address address = new Address("1-93","ABC","HYD",123456);
    return new User(name, 29, "Siva@gmail.com", new Date(), address);
  }
}

Output:

Source : User(name=Name(firstName=Siva, lastName=Prasad), age=29, email=Siva@gmail.com, dob=Tue Sep 26 14:38:45 IST 2017, address=Address(houseNo=1-93, street=ABC, city=HYD, pincode=123456))

Destination : UserDTO(firstName=Siva, lastName=Prasad, age=29, email=Siva@gmail.com, dob=Tue Sep 26 14:38:45 IST 2017, houseNo=null, street=null, city=null, pincode=null)

I am taking 2 reference types Name and Address in User.java. While creating object for User , I am passing both Name and Address details as well. When I try to map User object to UserDTO, Name details are getting mapped successfully, but Address details are not getting mapped.

Can any body help me in understanding why its happing like that or am I missing any thing?

1 Answers1

0

With MatchingStrategies.LOOSE everything works well.

The Loose matching strategy allows for source properties to be loosely matched to destination properties by requiring that only the last destination property in a hierarchy be matched. The following rules apply:

  • Tokens can be matched in any order
  • The last destination property name must have all tokens matched
  • The last source property name must have at least one token matched

The loose matching strategy is ideal to use for source and destination object models with property hierarchies that are very dissimilar. It may result in a higher level of ambiguous matches being detected, but for well-known object models it can be a quick alternative to defining mappings.

In this way it is necessary to add only one line:



    @Test
    public void shouldPopulateAllSimpleProperties() {
        User user = createUser();
        ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.LOOSE);
        UserDTO userDTO = modelMapper.map(user, UserDTO.class);

        System.out.println("Source : " + user);
        System.out.println("Destination : " + userDTO);
    }

Output:

Source : User{name=Name{firstName='Siva', lastName='Prasad'}, age=29, email='Siva@gmail.com', dob=Wed Oct 18 23:44:25 MSK 2017, address=Address{houseNo='1-93', street='ABC', city='HYD', pincode=123456}}
Destination : UserDTO{firstName='Siva', lastName='Prasad', age=29, email='Siva@gmail.com', dob=Wed Oct 18 23:44:25 MSK 2017, houseNo='1-93', street='ABC', city='HYD', pincode=123456}
Woland
  • 623
  • 2
  • 13
  • 31