JAXB Mapping cyclic references to XML

后端 未结 5 854
醉酒成梦
醉酒成梦 2020-11-27 05:38

I have an object graph that contains a cycle. How do I get JAXB to handle this? I tried using the @XmlTransient annotation in the child class but the JAXB mar

相关标签:
5条回答
  • 2020-11-27 06:22

    We can use XStream library as well, I tried it one project where JAXB was giving cyclic error but XStream handled it successfully

    0 讨论(0)
  • 2020-11-27 06:27

    The good thing about using JAXB is that it is a standard runtime with multiple implementations (just like JPA).

    If you use EclipseLink JAXB (MOXy) then you have many extensions available to you for handling JPA entities including bi-directional relationships. This is done using the MOXy @XmlInverseReference annotation. It acts similar to @XmlTransient on the marshal and populates the target-to-source relationship on the unmarshal.

    http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JPA/Relationships

    @Entity 
    @XmlRootElement 
    public class Contact { 
    
        @Id 
        private Long contactId; 
    
        @OneToMany(mappedBy = "contact") 
        private List<ContactAddress> addresses; 
    
    ... 
    
    } 
    
    @Entity 
    @XmlRootElement 
    public class ContactAddress { 
    
        @Id 
        private Long contactAddressId; 
    
        @ManyToOne 
        @JoinColumn(name = "contact_id") 
        @XmlInverseReference(mappedBy="addresses")
        private Contact contact; 
    
        private String address; 
    
    ... 
    
    } 
    

    Other extensions are available including support for composite keys & embedded key classes.

    To specify the EcliseLink MOXy JAXB implementation you need to include a jaxb.properties file in with your model classes (i.e. Contract) with the following entry:

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    
    0 讨论(0)
  • 2020-11-27 06:28

    XMLTransient almost always works for cycles. It might be a possibility that you have XMLTransient on the field level but you have not specified XMLAccessorType to be XmlAccessType.Field. If you don't specify anything the default is XmlAccessType.Property - or your getters. I have experienced Jaxb picking xml elements from getters from a class that I missed the accessor type annotations on and still work perfectly fine.

    0 讨论(0)
  • 2020-11-27 06:32

    This page in the "Unofficial JAXB Guide" offers three strategies for dealing with cycles. They are (in summary):

    • Mark one of the reference attributes that form the cycle as @XmlTransient.
    • Use @XmlID and @XmlIDREF so that the references are represented using XML ids arather than by containment.
    • Use the CycleRecoverable interface to deal with cycles programmatically.
    0 讨论(0)
  • 2020-11-27 06:33

    just look at this tutorial : Mapping cyclic references to XML by jaxb

    I use it an it works well :)

    0 讨论(0)
提交回复
热议问题