Failed solutions:
- JAXB's Unofficial Guide has some good suggestions, but they require a fixed parent-child relationship.
- Castor handles cycles out of the box, but there's data loss in that it refuses to preserve back references.
- This guy has an interesting solution that handles marshalling extremely well, but, at least just going off the code posted, unmashalling will never work.
Here's my hacky, yet functional, solution:
First, define an abstract class as follows:All of your domain objects must extend this class. It provides the IDs that will essentially be used as surrogate keys when your objects are marshalled. As you go through your classes extending Marshallable, annotate all of your properties with @XmlIDREF. For example:
Now, when JAXB marshals these objects it's going to simply insert references, which solves the cycle problem. However, it creates a new containment issue. If you were to marshal your objects now, you'd generate a well formed XML tree full of references that point nowhere. I'm not convinced this is the best solution, but here's my crack at the issue of containment.
You'll need to wrap every object that you marshal in this wrapper. It flattens the entire graph so that ever instance of Marshallable is a sibling.
Here's an example marshal:
Here's an example unmarshal:
It isn't pretty, but that should do the trick.
No comments:
Post a Comment