简体   繁体   中英

Copy complex object hierarchy in Java

I have a complex object hierarchy that has a couple of extends.

class One { String name; Color color; List clothes; }

class Two extends One { Test test; }

class Foo { One One; Two two; }

I want to copy all the attributes of a object of class One into a new object of class say another. How to do this ? Is there any library available ?

Here are a couple:

Dozer is pretty fast and has a good number of configuration options. Orika is supposedly very fast though I have not indecently confirmed that (check out the benchmarks they posted on their site).

Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another.

You can define mappings for copying object fields programmatically, with annotations or XML.

Wel, I have used both orika and dozer, I can say orika is at least 10 times faster than dozer in my project after the replacement. And also orika requires less jvm heap memories. orika is faster is beacause it compile the mapping rules as a runtime java class to do the properties mapping instead of looking into the mapping rules in runtime and using java reflection to get and set the properties values in runtime.

There's another framework that is even faster than Orika, take a look there - http://inspire-software.com/confluence/display/GeDA/Benchmarks

My simple test with 4 fields in a class and 1000000 copy operations took 100ms for GeDA, 1000ms for Orika and 4000ms for Dozer

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