ModelMapper mapper.skip() doesn't work for pojo objects with circular dependency
See original GitHub issueI have two pojo objects: Husband, Wife which reference each other.
Husband.java
public class Husband {
private String name;
private int age;
private String man;
private Wife wife;
// getter, setter, builder, constructor are removed for berevity
}
Wife.java
public class Wife {
private String name;
private int age;
private String woman;
private Husband husband;
// getter, setter, builder, constructor are removed for berevity
}
I’ve created simple typeMap rulings for both objects, where the referenced object is skipped.
My test class:
public class ModelTest {
@Test
public void test() {
ModelMapper modelMapper = new ModelMapper();
TypeMap<Wife, Wife> typeWife = modelMapper.createTypeMap(Wife.class, Wife.class);
typeWife.addMappings(mapper -> {
mapper.skip(Wife::setHusband);
});
TypeMap<Husband, Husband> typeHusband = modelMapper.createTypeMap(Husband.class, Husband.class);
typeHusband.addMappings(mapper -> {
mapper.skip(Husband::setWife);
});
Wife wife = Wife.builder().age(25).name("Sarah").woman("good woman").build();
Husband husband = Husband.builder().age(28).name("Imtiaz").man("good man").build();
wife.setHusband(husband);
husband.setWife(wife);
Husband updatedHusband = Husband.builder().age(28).name("Imtiaz Shakil").man("slightly good man").build();
modelMapper.map(updatedHusband, husband);
System.out.println(husband.toString());
System.out.println(husband.getWife().toString());
}
}
When mapping updatedHusband to husband, setWife() method is not skipped. But, if I remove typeWife mapping from modelMapper the code works fine.
I’m using ModelMapper 1.1.3
Thanks.
Issue Analytics
- State:
- Created 5 years ago
- Comments:16 (9 by maintainers)
Top Results From Across the Web
ModelMapper mapper.skip() doesn't work for pojo objects with ...
I've created simple typeMap rulings for both objects, where the referenced object is skipped. My test class: public class ModelTest { @Test ...
Read more >Guide to Using ModelMapper - Baeldung
In this tutorial, we're going to show how to map our data between differently structured objects in ModelMapper.
Read more >Getting Started - ModelMapper
First we have to let ModelMapper know about the types we want to validate. We can do this by creating a TypeMap :...
Read more >Prevent Circular Reference With Modelmapper List - ADocLib
I'm using Automapper to map my NHibernate proxy objects DTO to my CSLA business Does anyone know if Automapper can avoid this circular...
Read more >tommi ratamaa generic data transfer object and entity mapping
typically working as an Object Relation Mapping (ORM) [2] style meta ... with a circular annotation reference is prohibited by the compiler.
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
Please take a look #337 and GH336.java#L100-L119 that I want to write a test to reproduce your issue with old way.
That’s what my solution want to solve. Currently, modelmapper don’t have any approach to do this.