Developing JAXB Applications Using EclipseLink MOXy, Release 2.7
  Go To Table Of Contents

Multiple Mappings for a Single Property

Standard JAXB can have at most one mapping per Java field. Since EclipseLink MOXy 2.3, multiple mappings can be created for a single property using OXM metadata, with the caveat that at most one mapping will be readable (the rest will be "write-only").


This example will use the following Java class:

Example 4-37 Sample Java Class

package example;
import javax.xml.bind.annotation.*;
public class CustomQuoteRequest {
   private int requestId;
   private String currencyPairCode;

Example 4-38 shows how to define multiple mappings for the currencyPairCode in EclipseLink's OXM metadata format. Note that the second mapping has specified write-only="true".

Example 4-38 Sample XML Schema

<java-type name="CustomQuoteRequest">
      <xml-element java-attribute="requestId" name="id"/>
      <xml-attribute java-attribute="currencyPairCode" xml-path="req/info/instrmt/@sym"/>                            
      <xml-attribute java-attribute="currencyPairCode" xml-path="req/info/leg/token/@sym" write-only="true"/>           

XML Output

Example 4-39 shows an example CustomQuoteRequest marshalled to XML.

Example 4-39 Resulting XML

<?xml version="1.0" encoding="UTF-8"?>
         <instrmt sym="CAD/USD"/>
            <token sym="CAD/USD"/>