Search code examples
javadtoorika

How can Orika map objects without instantiating a new destination object


I need to replace Dozer mappings by Orika and I want to know if it's possible to do this Dozer stuff mapper.map(obj1, obj2) in Orika? I have seen so far that for destination you can only specify the class type and not an object, so I am assuming that a new instance will get created each time. But in this case I need to only update a previously found entity object with some fields of the DTO. Is that possible?


Solution

  • Yes, it is possible to both map source object A to a target class B - then Orika will instantiate B by itself - or to an already created instance of B.

    // Let Orika create instance of B
    A source = new A();
    BoundMapperFacade<A, B> mapper = mapperFactory.getMapperFacade(A.class, B.class);
    B target = mapper.map(source);
    
    // Create instance of B yourself and let Orika fill it
    A source = new A();
    B target = new B();
    BoundMapperFacade<A, B> mapper = mapperFactory.getMapperFacade(A.class, B.class);
    mapper.map(source, target);
    

    In the second case you can set up the target instance yourself in some way - Orika will only map the properties from A to B that you defined in the ClassMap.