views:

1967

answers:

3

In our project we have to map one nested structures of beans onto another. (These are actually JAXB mapped Java-Representations of XML documents that, say, represent an incoming order document.) This has to be mapped onto the quite different order document structure of another system.

What are the options to do this? I would prefer something that fulfills the following requirements:

  1. The mapping should alert me when the mapping of one field was not defined
  2. The mapping should have some defaults, like mapping fields of equal names onto one another and providing standard mappings for, say, int to String and vice versa.
  3. The mapping should be bidirectional.
  4. One should be able to use code completion when defining the mapping.

A promising framework for this is Dozer, but it does not fulfill 1 and 4. Same with JBeanMapper. Just programming it in Java does 4 but not the other requirements; using XSLT fulfills perhaps 2 but nothing else. Do you have better ideas?

+1  A: 

When we've had this problem, we've ended up doing field mappings in Java in a utility class. It's a real hassle, especially when you have to map to several different web services and have to write mappings for each of them (some of which are a simple 2D map of named attributes rather than a hierarchy of objects, cries).

However in that way you do have the power to analyse the requirements of the target of the mapping to get the best quality mapping, setting defaults where data isn't set, and so on. You can throw a custom "UndefinedMappingException" in your mapper where you need to. And by not being a fluffy library that uses reflection and/or complex XML mapping configuration files, it's a darn sight faster.

I.e., this is the post arguing for "write it in Java".

JeeBee
+1  A: 

I solved a similar issue with commons-beanutils.

My final goal was a big Java file which would build the data structure and the mapper created that file for me. This way, I could create test data from snapshots made while the program was running.

The mapper allowed to define the keys by which to sort the objects, the field to use to generate object names in the Java file and I used a map with "class:field" as the key. The value was an object implementing a simple "Mapping" interface (one method: toJava(Object instance, String field, Object value)).

My main problem was to compile 2MB+ Java files :)

Aaron Digulla
A: 

Can you please provided more details on how it has been resolved

Since this is not an answer, it should probably be a comment at the question. ;-) For now we have not resolved this but did everything by hand, since we did not find a better solution than that.
hstoerr