2

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?

julien
  • 898
  • 3
  • 17
  • 32

2 Answers2

3

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:

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400
0

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; }
Florent
  • 12,310
  • 10
  • 49
  • 58