EclipseLink Solutions Guide for EclipseLink
Release 2.4
  Go To Table Of Contents
 Search
 PDF

Using XML Metadata Representation to Override JAXB Annotations

In addition to using Java annotations, EclipseLink provides an XML mapping configuration file called eclipselink-oxm.xml that you can use in place of or to override JAXB annotations in the source with an XML representation of the metadata. In addition to allowing all of the standard JAXB mapping capabilities it also includes advanced mapping types and options.

An XML metadata representation is useful when:

This section demonstrates how to use eclipselink-oxm.xml to override JAXB annotations


NoteNote:

While using this mapping file enables many advanced features, it might prevent you from porting it to other JAXB implementations


Task 1: Define Advanced Mappings in the XML

First, update the XML mapping file to expose the eclipselink_oxm_2_3.xsd. schema. Example 15-20 shows how to modify the <xml-bindings> element in the mapping file to point to the correct namespace and leverage the schema. Each Java package can have one mapping file.

Example 15-20 Updating XML Binding Information in the Mapping File

<?xml version="1.0"?>
<xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/oxm  http://www.eclipse.org/eclipselink/xsds/eclipselink_oxm_2_4.xsd"
       version="2.4">
</xml-bindings>

Task 2: Configure Usage in JAXBContext

Next, pass the mapping file to JAXBContext in your object:

  1. Specify the externalized metadata by inserting this code:

    Map<String, Object> properties = new HashMap<String, Object>(1);
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "org/example/oxm.xml);
    JAXBContext.newInstance("org.example', aClassLoader, properties);
    
  2. Create the properties object to pass to the JAXBContext. For this example:

    Map<String,Object> properties = new HashMap<String,Object>();
    properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, metadata);
    
  3. Create the JAXBContext. For this example:

    JAXBContext.newInstance("example.order:example.customer", aClassLoader, properties);
    

Task 3: Specify the MOXy as the JAXB Implementation

You must use MOXy as your JAXB implementation. To do so, do the following:

  1. Open a jaxb.properties file and add the following line:

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    
  2. Copy the jaxb.properties file to the package that contains your domain classes.