Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map collections in Dozer

Tags:

I'd like to do something like:

ArrayList<CustomObject> objects = new ArrayList<CustomObject>(); ... DozerBeanMapper MAPPER = new DozerBeanMapper(); ... ArrayList<NewObject> newObjects = MAPPER.map(objects, ...);  

Assuming:

<mapping>   <class-a>com.me.CustomObject</class-a>   <class-b>com.me.NewObject</class-b>        <field>         <a>id</a>         <b>id2</b>       </field>   </mapping> 

I tried :

ArrayList<NewObject> holder = new ArrayList<NewObject>(); MAPPER.map(objects, holder); 

but the holder object is empty. I also played with changing the second argument without any luck...

like image 489
Stephane Grenier Avatar asked Aug 31 '09 17:08

Stephane Grenier


People also ask

How does Dozer mapping work?

Dozer automatically maps all fields with the same property name from the source object into the destination object. We will then place our custom XML file on the classpath directly under the src folder.

Does Dozer use reflection?

Dozer uses reflection to access data object properties, so it is designed to work with data objects that have corresponding getter and setter methods for its fields.

What is bean mapping?

It is mainly bean to bean mapper that recursively copies data from one java object to another java object – attribute by attribute. We realize it's full capability when We are dealing with deeply-nested complex java beans, which we are easily seen in large enterprise applications.

What is Net SF Dozer?

net.sf.dozer » dozer-parentApache. Dozer is a powerful Java Bean to Java Bean mapper that recursively copies data from one object to another. Last Release on Apr 22, 2014.


1 Answers

To quote:

"Nested collections are handled automatically, but you are correct that top level collections need to be iterated over. Currently there isn't a more elegant way to handle this."

Someone has figured a way to do it without a looping construct in your code base, but I think it's just easier (and more readable/maintainable) to put it in your code. Hopefully they'll add this ability sooner than later.

like image 89
Stephane Grenier Avatar answered Sep 28 '22 10:09

Stephane Grenier