Map Flat Structure to Hierarchial Structure using ModelMapper
See original GitHub issueThe below class is my Target:
I have a Employers class that has a list of Employer. Employer class has employer attributes and a list of employees.
public class Employers {
List<Employer> employer;
//getters and setters here
}
public class Employer {
String employerId;
String employerName;
List <Employee> employee;
//getters and setters
}
public class Employee {
String employeeId;
String employeeName;
//getters and setters
}
Now my source is a flat structure:
I have a class EmployersDTO which has list of EmployerDTO. EmployerDTO has employerid, employername, employeeid and employeename attribute.
It looks like below:
public class EmployerDTO {
String employerId;
String employerName;
String employeeId;
String employeeName;
//getters and setters here
}
public class EmployersDTO{
List <EmployerDTO> employerDTO;
//getters and setters here
}
Is this kind of mapping from the above source to target possible. How to do this flat structure to hierarchial mapping?
EmployersDTO -> Employers
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
ModelMapper mapping from multi flat object to hierarchy object
I have a situation, where I need to map multi objects (in a flat structure) into one object (an hierarchy object) in Java...
Read more >Flattening - ModelMapper
ModelMapper is an intelligent, refactoring safe object mapping library that automatically maps objects to each other. It uses a convention based approach ...
Read more >Guide to Using ModelMapper - Baeldung
Learn how to map our data between differently structured objects using ModelMapper by creating custom class-to-class mappings with property ...
Read more >One-Stop Guide to Mapping with MapStruct - Reflectoring
Sometimes we would like to implement a specific mapping manually by defining our logic while transforming from one object to another. For that, ......
Read more >Dto Mapping - unica e sensual
Option 4: Use a mapping library like MapStruct MapStruct is a really nice model mapping ... They are flat data structures that contain...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
ModelMapper is working well for flatten-to-hierarchical(example) / hierarchical-to-flatter(example), but it can’t not work with many-to-one case.
So I’m not sure is there many cases like your Employer example, is Order same?
Another idea for your employer case, maybe you can try Have employer field in your employee class, like
Then you can mapped List<EmployerDto> to List<Employee> without any configuration. But it works or not depends on your use case.
I took Employee and Orders for example for the POC i am doing with ModelMapper.
So, Here is what i am trying to see if ModelMapper can help achieve.
Let us take a look at the map() function. It takes a source object as the first argument and a class which is our target as the second argument. I should be able to expose an API which uses the map() and map the source object and set the target implicitly automatically.
The only thing that is constant is, the kind of source and target.
My source object is typically always is a flattened structure having all the attributes at the same level.
My target class is a structured or hierarchical structure, much like the one we had in the Employer example.