Dozer Mapping
RodneyEberly
Note from the editor: This post was made on 08/31 but we did not see it pending until now. The post is valid for the 08.2011 Great Tech Blog-Off Contest.
If anyone of you programs in Java, I suggest you check out Dozer. http://dozer.sourceforge.net/
It is a Java bean mapper that copies one Java bean’s data to another bean. We have been using it in production at work for over a year, and have been very pleased with it. We store our data in xml, and use Castor (another very useful tool) to create a Java bean representation of the xml, based off of a schema. However, our site has its own web beans, so we needed a way to map the data back and forth between the Castor beans and the web beans.
Dozer is a powerful tool for programming the mapping back and forth very quickly. We have been using the Dozer’s XML mapping. Basically you define the two objects to map between in your mapping xml file, and then define the desired field to field mapping. The mapping file is bidirectional, so you only have to define everything once.
There are many nice features available. If the field name (ie. first name) is the same in both the source bean and destination bean, you don’t even need to specify it. Dozer will automatically take care of it. If that specific mapping is not desired, you can always exclude. It is bi-directional by default, but you can also specify one-way mapping only, if needed. It will automatically map collections, arrays, primitives to primitives, Strings to primitives, and many more. You can specify field mappings that are multiple levels deep. If you need more elaborate mapping, (ie. manipulating data, calculations, etc.), you can always specify your own custom mapper (java class file).
So if you stuck through this article, followed it, and program (not drink) Java, check Dozer out!
