Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not work for self reference #192

Closed
tuonglx opened this issue Oct 22, 2016 · 1 comment
Closed

Not work for self reference #192

tuonglx opened this issue Oct 22, 2016 · 1 comment

Comments

@tuonglx
Copy link

tuonglx commented Oct 22, 2016

It works well for

public class MainTest {
    public static void main(String[] args) {
        ModelMapper modelMapper = new ModelMapper();
        Parent p = new Parent();
        p.name = "1";
        Order order = new Order();
        order.name = "2";
        order.parent = p;
        OrderDTO dto = modelMapper.map(order, OrderDTO.class);
        System.out.println(dto.parentName);
    }
}
class Order {
    String name;
    Parent parent;

}

class Parent {
    String name;
}

class OrderDTO {
    String name;
    String parentName;
}

but does not work for

class Order {
    String name;
    Order parent;
}
@chhsiao90
Copy link
Member

@tuonglx - Sorry for the lately reply...
I think self reference is always complicated and not easy to handle.
Maybe you can try our explicit mappinp.

If you are using ModelMapper v1.0.0 with java 8, then you can do this.

modelMapper.typeMap(Order.class, OrderDTO.class).addMapping(
  src -> src.getParent().getName(),
  (dest, value) -> dest.setParentName(value));

Or for old school style, you can try this

modelMapper.addMappings(new PropertyMap<Order.class, OrderDTO.class>() {
  protected void configure() {
    map().setParentName(source.getParent().getName());
  }
});

And thanks for the report! I will close this issue, and feel free to leave comment if you still have some problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants