Skip to main content
Jump to: navigation, search


EclipseLink JAXB (MOXy) provides an XML mapping document as an alternative to using annotations. This mapping document can be provided in many different forms: resource path, URL, File, Source, etc. If the metadata is in some sort of repository this flexibility might not be enough. To handle this use case in EclipseLink 2.3 we have introduced a new mechanism called MetadataSourceAdapter. You simply need to extend this class and implement the getXmlBindings(Map, ClassLoader) method. This method returns an instance of XmlBindings which is a JAXB model for the XML mapping document. You can either create this model by hand, or use the JAXB APIs to populate it (as shown below).

MetadataSourceAdapter Subclass

package examples.metadatasource;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import org.eclipse.persistence.jaxb.metadata.MetadataSourceAdapter;
import org.eclipse.persistence.jaxb.xmlmodel.XmlBindings;
public class ExampleMetadataSource extends MetadataSourceAdapter {
    private JAXBContext xmlBindingsContext;
    public ExampleMetadataSource() throws JAXBException {
        xmlBindingsContext = JAXBContext.newInstance("org.eclipse.persistence.jaxb.xmlmodel");
    public XmlBindings getXmlBindings(Map&String, ?> properties, ClassLoader classLoader) {
        try {
            URL xmlBindings = classLoader.getResource("examples/metadatasource/binding.xml");
            return (XmlBindings) xmlBindingsContext.createUnmarshaller().unmarshal(xmlBindings);
        } catch(JAXBException e) {
            throw new RuntimeException(e);

Mapping File

The mapping file contains the same information as the JAXB annotations. Like with annotations you only need to specify metadata to override default behavior. For more information on how to use and specify a bindings file see the EclipseLink-OXM.XML example

<?xml version="1.0"?>
        <java-type name="Customer">
                <xml-element java-attribute="firstName" name="first-name"/>
                <xml-element java-attribute="lastName" name="last-name"/>
                <xml-element java-attribute="phoneNumbers" name="phone-number"/>
        <java-type name="PhoneNumber">
                <xml-attribute java-attribute="type"/>
                <xml-value java-attribute="number"/>

Java Model

The following domain model will be used in this example. Because the JAXB metadata is represented as XML, no annotations are used on the classes. The get/set methods have been omitted to save space.


package examples.metadatasource;
import java.util.List;
public class Customer {
    private String firstName;
    private String lastName;
    private Address address;
    private List<PhoneNumber> phoneNumbers;


package examples.metadatasource;
public class Address {
    private String street;


package examples.metadatasource;
public class PhoneNumber {
    private String type;
    private String number;

JAXBContext Configuration

The XML metadata source is passed in via the properties parameter when the JAXBContext is instantiated.

Map<String, Object> properties = new HashMap<String, Object>(1);
MetadataSource metadataSource = new ExampleMetadataSource();
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, metadataSource);
JAXBContext jc = JAXBContext.newInstance(new Class[] {Customer.class}, properties);

Example Output

Using the given mappings file the following XML can be successfully unmarshalled.

Unmarshaller unmarshaller = jc.createUnmarshaller();
Customer customer = (Customer) unmarshaller.unmarshal(new File("src/examples/metadatasource/input.xml"));
<source lang="xml">	
<customer xmlns="">
        <street>123 A Street</street>
    <phone-number type="work">555-1111</phone-number>
    <phone-number type="cell">555-2222</phone-number>

Back to the top