question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Map Flat Structure to Hierarchial Structure using ModelMapper

See original GitHub issue

The 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:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
chhsiao90commented, Apr 10, 2018

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

public class Employee {
  Employer employer;
  String employeeId;
  String employeeName;
}

Then you can mapped List<EmployerDto> to List<Employee> without any configuration. But it works or not depends on your use case.

0reactions
anbosscommented, Apr 10, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found