Question

I use JAXB marshaller to store some java objects as XML files. Some of these objects reference each other, so I unsurprisingly obtain this error:

[com.sun.istack.internal.SAXException2: A cycle is detected in the object graph. This will cause infinitely deep XML

The solution which consists in removing the cycles and use only tree structure is not feasible - I need both navigability directions.

To solve this issue, I would rather use xlink to reference the xml objects instead of copying them in cascade. Is this solution pertinent? Is it possible to do that with JAXB marshaller? How?

Was it helpful?

Solution

You can implement an XLink approach in JAXB using an XmlAdapter. Below are links to various similar answers.

I lead the EclipseLink JAXB (MOXy) implementation, and we have the @XmlInverseReference extension for mapping bidirectional relationship that you may be interested in:

OTHER TIPS

Dude, you can note in one of the entity the annotation @XmlTransient, so when unmarch, it will not complain about the cycle problem.

But with thius workaround after unmarch the xml you will have to populate the atribute with the @XmlTransient.

I was reading some paper and find this. You can set @XmlTransient and use the callback method to do something after the unmarch. So you can set the parent to you child.

public void afterUnmarshal(Unmarshaller u, Object parent) {
    this.pessoa = (Pessoa) parent; }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top