Jump to: navigation, search

EclipseLink/UserGuide/MOXy/Simple Values/Multiple Mappings

< EclipseLink‎ | UserGuide‎ | MOXy‎ | Simple Values
Revision as of 15:14, 20 June 2011 by Rick.barkhouse.oracle.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug Day
Contribute
Browse Source

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").


Example

This example will use the following Java class:

package example;
 
import javax.xml.bind.annotation.*;
 
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class CustomQuoteRequest {
 
   private int requestId;
 
   private String currencyPairCode;
 
}

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

...
<java-type name="CustomQuoteRequest">
   <xml-root-element/>
   <java-attributes>
      <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"/>           
   </java-attributes>
</java-type>
...


XML Output

The output below shows an example CustomQuoteRequest marshalled to XML.

<?xml version="1.0" encoding="UTF-8"?>
<customQuoteRequest>
   <id>881</id>
   <req>
      <info>
         <instrmt sym="CAD/USD"/>
         <leg>
            <token sym="CAD/USD"/>
         </leg>
      </info>
   </req>
</customQuoteRequest>