Processing of object members in JAXB

Source: Internet
Author: User
Today, my colleague consulted me about JAXB's handling of the problem, why I always reported infinite loop in soap, and I explained that the problem arises because 2 objects are referred to each other () in the work code is a complex a onetomany B, b manytoone A, a The relationship between Onetomany A, a manytoone A, causes a dead loop when parsing the XML, and in order to solve the dead loop simply by disconnecting the reference at one end, JAXB provides the corresponding annotation to solve the problem, but when we Xmltransient added in the field above but found that does not work, or run infinite loop exception, surf the internet Google A, did not find any problems, and later went to JAXB's website to see, found that the effective sex of annotation is not stated in the lot, After solving the problem, it is found that @XmlTransient directly remove the reference, use the trouble, return the object from the XML when you need to add back the member object, @XmlInverseReference simplify the related operation, recommend use.

Reference:
Https://jaxb.java.net/tutorial/index.html
Http://blog.bdoughan.com/2010/07/jpa-entities-to-xml-bidirectional.html
Http://blog.bdoughan.com/2013/03/moxys-xmlinversereference-is-now-truly.html
Top-level Elements: xmlrootelement

A class that describes an XML element, "is" to be a top-level element, i.e., one of the can function as an XML document, s Hould is annotated with xmlrootelement. Its optional elements are name and namespace. By default, the class name is used as the name

Xmlaccessortype
Can annotate a package or a top level class with Xmlaccessortype, setting its value element to one of the enum Constan TS FIELD, property, Public_member (default) or NONE. The If field is set every non static, non transient FIELD would be automatically bound. Property instructs the JAXB to does this for getter and setter pairs. NONE suppresses bind except for explicitly annotated fields or properties. A class Without this annotation inherits the Xmlaccessortype setting either to its superclass or from the package G.

FIELD
Every non static, non transient field in a Jaxb-bound class would be automatically bound to XML, unless annotated by Xmltra Nsient.
NONE
None of the fields or properties is bound to XML unless they are specifically annotated with some of the JAXB annotations.
Property
Every Getter/setter pair in a Jaxb-bound class'll is automatically bound to XML, unless annotated by xmltransient.
Public_member
Every public Getter/setter pair and Every public field is automatically bound to XML, unless annotated by Xmltransien T.

The other annotation to being mentioned in the and Xmltransient. It suppresses binding for its target which can is a entire class or a field or a method. This is also useful if your have a name clash resulting from a public field, say Foo, and methods Getfoo and Setfoo.
Javax.xml.bind.annotation
Annotation Type Xmlaccessortype

@Inherited
@Retention (Value=runtime)
@Target (Value={package,type})
Public @interface Xmlaccessortype

Controls whether fields or Javabean properties are serialized by default.

Usage

@XmlAccessorType annotation can be used with the following program elements:

Package
A top level class

"Package specification" in Javax.xml.bind.package Javadoc for additional common information.

This is annotation provides control over the default serialization to properties and fields in a class.

The annotation @XmlAccessorType on a package applies to all classes in the package. The following inheritance semantics apply:

If there is a @XmlAccessorType in a class, then it is used.
Otherwise, if a @XmlAccessorType exists on one of their super classes, then it is inherited.
Otherwise, the @XmlAccessorType on a package is inherited.

Defaulting Rules:

By default, if @XmlAccessorType in a package is absent, then the following package level annotation is assumed.

@XmlAccessorType (Xmlaccesstype.public_member)


By default, if @XmlAccessorType on a class are absent, and none of its super classes are annotated with @XmlAccessorType, th En the following default on the class is assumed:

@XmlAccessorType (Xmlaccesstype.public_member)

Example
1.
@XmlAccessorType (xmlaccesstype.public_member) public
class SomeClass {
    private String A;
    Private String B;

    Public SomeClass () {...}

    Public String Geta () {...}
    public void SetA (String value) {...}

    @XmlTransient//transient should mentioned on Get/set A or public field for Public_memeber type public
    String get B () {...}
    public void Setb (String value) {...}
}}
Package blog.inversereference;
 
Import javax.xml.bind.annotation.*;
Import org.eclipse.persistence.oxm.annotations.XmlInverseReference;
 
@XmlRootElement
@XmlAccessorType (xmlaccesstype.field) public
class Customer {
 
    String name;
 
    @XmlElement
    @XmlInverseReference (mappedby= "customer")
//transient/xmlinversereference should metioned On private field as field type address
    ;
 


Address

Here's the @XmlInverseReference annotation is used to specify so when the ' address ' is populated on the Customer ob Ject, the Customer property on the Address object is set as a back pointer.

Package blog.inversereference;
 
Import javax.xml.bind.annotation.*;
Import org.eclipse.persistence.oxm.annotations.*;
 
 
@XmlRootElement
@XmlAccessorType (xmlaccesstype.field) public
class Address {
 
    String Street;
 
    String City;
 
    @XmlElement
    @XmlInverseReference (mappedby= "address")
    customer customer;
 
}

Import javax.persistence.*;
 
@Entity public
class Customer {
 
    @Id
    private long Id;
 
    @OneToOne (mappedby= "Customer", Cascade={cascadetype.all})
    private address address;
    
Import javax.persistence.*;
 
@Entity public
class address implements Serializable {
 
    @Id
    private long Id;
 
    @MapsId
    @OneToOne
    @JoinColumn (name= "ID")
    private customer customer;
 
}


Using Vanilla Jaxb to marshal these objects to XML for you need to mark One Direction @XmlTrasient, this prevents Jaxb from en  Tering an infinite loop during marshalling (Object-to-xml). During unmarshalling (Xml-to-object) However you are responsible for populating to the back pointer yourself.
Import javax.persistence.*;
Import javax.xml.bind.annotation.*;
 
@Entity
@XmlRootElement
@XmlAccessorType (xmlaccesstype.field) Public
class address implements Serializable {
 
    @Id
    private long Id;
 
    @OneToOne
    @JoinColumn (name= "ID")
    @MapsId
    @XmlTransient
    private customer customer;
 
}


Moxy offers a extension that would populate the back pointer for your, this is done with the @XmlInverseReference Annotatio N. How @XmlInverseReference annotation leverages the same "Mappedby" concept.
Import javax.persistence.*;
Import org.eclipse.persistence.oxm.annotations.*;
 
@Entity
@XmlRootElement
@XmlAccessorType (xmlaccesstype.field) Public
class address implements Serializable {
 
    @Id
    private long Id;
 
    @OneToOne
    @JoinColumn (name= "ID")
    @MapsId
    @XmlInverseReference (mappedby= "address")
    private Customer customer;
 
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.