简体   繁体   中英

Mapping two identical classes in java with third party library

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.

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>

最好使用适当的克隆库,请检查此http://blog.jadira.co.uk/blog/2013/6/25/announcing-jadira-cloning.html

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