Search code examples
mappingorika

Orika: mapping fields of 2 classes to one class


Is there a way to map fields from classes to one using Orika.
Can't find the solution in the orika documentation.

In the example the fields test & name from class ObjectOne should be mapped to the corresponding fields ObjectNew.

public class ObjectOne {

    private String test;
    private String name;
    private String id;

    public ObjectOne(String id,String test, String name){
        this.id=id;
        this.test=test;
        this.name=name;
    }
}

The same with field sheet from ObjectTwo

public class ObjectTwo {

     private String sheet;
     private String id;

     public ObjectTwo(String id,String sheet){
        this.id=id;
        this.sheet=sheet;
     }
}

Code for ObjectNew

public class ObjectNew {

    private String id;
    private String test;
    private String name;
    private String sheet;

    public ObjectNew(String id,String test,String name,String sheet){
       this.id=id;
       this.test=test;
       this.name = name;
       this.sheet = sheet;
    }
}

Fields from both classes ObjectOne & ObjectTwo should initiate new object ObjectNew when id's of classes ObjectOne and ObjectTwo are the same.

Any ideas how to handle this?

Kind Regards


Solution

  • I would suggest to wrap the source objects into one source wrapper object and map this new wrapper object with your new object:

    public class objectWrapper{
       private objectOne objectOne;
       private objectTwo objectTwo;
    }