'Add ModelMapper Maping from Entity Class to Dto Class with OneToMany relationship

I have that relationships (Spring Boot JPA Implementation):

One Person have multiples Addresses

One Address contains one Country

public class Person {
    
private Integer id;
private Integer name;
private Integer age;

//relations
}
  
        
public class Address {
    
private Integer id;
private Integer person_id;
private String street;
private String city;
private String country_id;
private Boolean preferred;

//relations
}
        
public class Country {
    
private Integer id;
private Integer description;

//relations

and i want return a DTO in that way

public class PersonDto {

 private Integer id;
 private Integer name;
 private Integer age;
 private String street;
 private String city;
 private Integer country //description;

but in fact i want

  1. only return preferred address of person (only can have one).
  2. Get only Country description in nested class of Address

It's possible with ModelMapper (AddMapping Strategy)

Thanks,



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source