Martin Schröder
Martin Schröder

Reputation: 4591

Migrating objects between different schema versions with JAXB

We are using JAXB to store serialized objects in a JCR. The XML files use versioned schemas. Obviously the classes of the objects will change eventually, so we'll have newer schema versions.

How can we automatically migrate the old stored objects to their newer versions? Does JAXB offer any support there or is it the wrong soultion? Currently we are using XSLT scripts to convert the XML files, but it would of course be much nicer if we could automagically create new object versions from the old serialized objects.

Expected changes to the objects can be everything: additions or removal of members, renaming of members and type changes of members.

Upvotes: 1

Views: 801

Answers (1)

Bill Horvath
Bill Horvath

Reputation: 1717

The trick is to make the newer schema versions backwards-compatible with previous schemas. (Note that this means your schemas will always get bigger.)

To eliminate an element or attribute in a new schema that was valid in the old schema, you make its use optional in the new schema (minOccurs="0" for elements, use="optional" for attributes.) This will allow data marshalled out under the old schema to be unmarshalled as valid under the new schema; you can then marshal the data back out under the new schema after having stripped out the offending element.

To add an element to the new schema that wasn't in the old schema, you follow the same logic: you make it optional in the new schema (which allows the old data to be unmarshalled as valid); you can then add the new element(s) or attribute(s) before you marshal it back out.

Upvotes: 1

Related Questions