简体   繁体   中英

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM