0

I need to convert one object to other .The object has nested of many inner objects.But both source and destination objects are identical by data type , variable name , class name etc. Is there any effective tool to do so with less coding.

I have tried orika but its expecting to define like below

factory.registerClassMap(factory
                .classMap

with all the fields and inner object fields.I ended up with lots of iterator and setting filed mappings.

Help me or guide me Is there any other way to map objects with identical objects.

Rithik_Star
  • 651
  • 5
  • 14
  • 39
  • if i understand correctly your issue , you want to clone an object that has inner references , you could try Apache commons or check [here](http://stackoverflow.com/questions/9264066/beanutils-clonebean-deep-copy) the answers , otherwise youll have to implement a custom DeepCopy method – AntJavaDev Jun 07 '16 at 06:47
  • No I dont want to clone the object .I have POJO and JAXB which are identical .I need to set the value from POJO to JAXB – Rithik_Star Jun 07 '16 at 06:50
  • ok then you have the pojo object and you will clone it to the JAXB object , if they are identical as you saying and only the package name changes – AntJavaDev Jun 07 '16 at 06:53

2 Answers2

0

You could have a look at MapStruct.

Another alternative is Dozer. Classes can be mapped 1 on 1 by defining in the mapper xml file attribute wildcard="true". This means that it will automatically try to map every property in the two objects. When the attribute is set to false it will only map explicitly defined fields.

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://dozer.sourceforge.net
          http://dozer.sourceforge.net/schema/beanmapping.xsd">

    <mapping wildcard="true"> 
        <class-a>org.example.ObjectA</class-a>
        <class-b>org.example.ObjectB</class-b>   
    </mapping> 
</mappings>
uniknow
  • 938
  • 6
  • 5
0

It's better to use a proper cloning library, check this one http://blog.jadira.co.uk/blog/2013/6/25/announcing-jadira-cloning.html

Sidi
  • 1,739
  • 14
  • 16