如何强制 JAXB 编组器使用 xlink 引用?
我使用 JAXB 编组器将一些 java 对象存储为 XML 文件。其中一些对象相互引用,所以我毫不奇怪地得到了这个错误:
[com.sun.istack.internal.SAXException2: A cycle is detected in the object graph. This will cause infinitely deep XML
删除循环并仅使用树结构的解决方案是不可行的 - 我需要两个导航方向。
为了解决这个问题,我宁愿使用xlink来引用xml对象,而不是级联复制它们。这个解决方案是否相关?是否可以使用 JAXB 编组器来做到这一点?如何?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
XmlAdapter
在 JAXB 中实现 XLink 方法。以下是各种类似答案的链接。我领导EclipseLink JAXB (MOXy) 实现,并且我们有用于映射您可能感兴趣的双向关系的
@XmlInverseReference
扩展: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:老兄,你可以在其中一个实体中注释
@XmlTransient
,这样在unmarch时就不会抱怨循环问题了。但是,在取消 xml 后使用此解决方法,您将必须使用
@XmlTransient
填充属性。我正在阅读一些论文并找到了这个。您可以设置@XmlTransient并使用回调方法在unmarch之后执行某些操作。所以你可以将父母设置为你的孩子。
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.