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.

ModelMapper mapper.skip() doesn't work for pojo objects with circular dependency

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
chhsiao90commented, May 26, 2018

Please take a look #337 and GH336.java#L100-L119 that I want to write a test to reproduce your issue with old way.

2reactions
chhsiao90commented, May 26, 2018

when creating typeHusband map, we first apply current mapping rules before performing implicit mapping.

That’s what my solution want to solve. Currently, modelmapper don’t have any approach to do this.

Read more comments on GitHub >

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

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