XML object, backward and forward compatibility

I work in an application where we need to save objects in XML format and load them later, as soon as necessary. To do this, I used JAXB to marshall and reverse route XML files back to Java classes.

My problem is that I sometimes have to change Java models (adding, renaming or deleting attributes), as a result I will have incompatible saved XML files that cannot be attached to the new form of the class.

To solve this problem, every time I have to make changes, I take a copy of all the classes in a new package (named after its version) and apply the requested changes. And when saving XML, I save its version so that I can decide which package JAXB needs to check in order to unmount this XML.

My question is: is there any other way to implement backward and forward compatibility using JAXB? If there is no other technology that can support this?

+8
java compatibility backwards-compatibility jaxb
source share
2 answers

Note: I am a member of the JAXB 2 Expert Group ( JSR-222 ) and lead the EclipseLink JAXB (MOXy) .

In this case, I prefer to use one model when possible. This will require multiple mappings for your object model. The JAXB specification does not provide a means to do this, but it can be done using MOXy's extra metadata extensions:

Metadata can be used to supplement annotations or to replace them. Therefore, I would recommend mapping the base schema to annotations and using the XML format to change metadata for each version of the schema.

My problem is that I have to change Java models someday (by adding, renaming or deleting attributes), as a result I will have incompatible saved XML files that cannot be associated with the new form of the class.

Removing the Java attribute (field / property) complicates the job because there will be nothing for the old XML. Instead, you can leave them in your model and mark them with "@XmlTransient" in the XML metadata files.

+5
source share

If you add only new attributes, it can still work: it is called duck print. Your object is free, ignoring unnecessary things that are not there.

You only need to worry about the version if you modify or delete the required attributes. Unfortunately, this applies to database schemas, Java serialization, and any other persistence technology. XML is not magic; he is not insured.

+1
source share

All Articles