JAXBElement is a JAXB (JSR-222) mechanism that stores name and namespace information in situations where this can not be determined from the value or mapping. For example in the class below the elements billing-address and shipping-address both correspond to the Address class. In order to be able to round trip the data we need to keep track of which element we unmarshalled.
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@XmlElementRefs({
@XmlElementRef(name = "billing-address"),
@XmlElementRef(name = "shipping-address")
})
private JAXBElement<Address> address;
}
While useful JAXBElement can get in the way if you want to use your domain model with something like JPA (which doesn't know what to do with it). In this post I will demonstrate how you can eliminate the need for JAXBElement through the use of an XmlAdapter.