Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/Development/339381"
Line 31: | Line 31: | ||
@XmlRootElement | @XmlRootElement | ||
public class Customer { | public class Customer { | ||
− | |||
@XmlAttribute | @XmlAttribute | ||
private String name; | private String name; | ||
Line 39: | Line 38: | ||
// getters and setters... | // getters and setters... | ||
− | |||
} | } | ||
</source> | </source> |
Revision as of 11:31, 24 March 2011
Design Specification: XML Flex Fields
Document History
Date | Author | Version Description & Notes |
---|---|---|
110323 | Rick Barkhouse | 1.00 |
Feature Overview
Currently, EclipseLink MOXy supports the mapping of Java fields and properties to XML. Said another way; in order to map data to XML, the user must have an existing Java field or property to map.
To support multi-tenancy, we will be allowing the user to add additional mappings at runtime. Because these new mappings would not have existing fields / properties on the Java class to map to, we will instead introduce the concept of a "flex field", where we can instead map the elements of a Java Map to the desired XML.
Example
The following domain class specifies an @XmlFlexField to hold additional mappings:
@XmlRootElement public class Customer { @XmlAttribute private String name; @XmlFlexField private Map<String, Object> flexField; // getters and setters... }
In an eclipselink-oxm.xml metadata file, we will define additional mappings that we would like to add to Customer:
... <java-types> <java-type name="Customer"> <java-attributes> <xml-element java-attribute="discountCode" name="discount-code" type="java.lang.String" /> </java-attributes> </java-type> </java-types> ...
To set the values for these additional mappings, we will add elements into the flexField Map:
InputStream oxm = classLoader.getResourceAsStream("eclipselink-oxm.xml"); Map<String, Object> properties = new HashMap<String, Object>(); properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, oxm); Class[] classes = new Class[] { Customer.class }; JAXBContext ctx = JAXBContext.newInstance(classes, properties); Customer c = new Customer(); c.setName("Dan Swano"); c.getFlexField().put("discountCode", "SIUB372JS7G2IUDS7"); ctx.createMarshaller().marshal(e, System.out);
This will produce the following XML:
<customer> <name>Dan Swano</name> <discount-code>SIUB372JS7G2IUDS7</discount-code> </customer>