Search code examples
javamappingorika

How to map fields using custom mapper and save fields names mapping in MapperFacade or MapperFactory?


There are two classes. One of them have field with FunctionalInterface type. I need to map field nameA from ClassA to base from ClassB. I can make it with custom mapper but then I will not have fields names mapping in MapperFacade (e.g. "nameA" <-> "base" in fieldsMapping Collection), but I need it for further mathing in other method. If I map them with fields names then I get an Exception that java can't map String to CustomFunctionalInterface. Is there something way to resolve it (map nameA into base and have fields names mapping in MapperFacade or MapperFactory)?

package project.my;

public class ClassA {
    String nameA;

    public String getNameA() {
        return nameA;
    }

    public void setNameA(String nameA) {
        this.nameA = nameA;
    }
}
package project.my;

public class ClassB {
    CustomFunctionalInterface base;

    public CustomFunctionalInterface getBase() {
        return base;
    }

    public void setBase(CustomFunctionalInterface base) {
        this.base = base;
    }
}
package project.my;

@FunctionalInterface
public interface CustomFunctionalInterface {
    String name();
}

Solution

  • I have searched resolution in orika documentation, but haven't found anything related to this question. I think there is no good way to resolve this issue. With customMappers you can't fill fieldsMapping with needed fields relationship. Similary to converters (for this case). My workaround:

    ...
       .fieldMap("nameA", "base").bToA().exclude().add()
    ...
    

    When you exclude mapping relationship will be written in fieldsMapping (but with "<-" relation instead of "<->"). In my case this exclusion doesn't have sense, but relationship will be existed in fieldMappind (or classMapping)