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 "User:Rick.barkhouse.oracle.com/Test1"
Line 10: | Line 10: | ||
= Virtual Access Methods = | = Virtual Access Methods = | ||
− | In addition to the standard JAXB access methods (public member, field, property, etc.), EclipseLink MOXy 2.3 introduces the concept of "virtual access methods", which instead rely on special '''get()''' and '''set()''' methods to maintain mapping data. For example, you might want to use a '''HashMap''' as the underlying data structure to hold all of your mapping data. | + | In addition to the standard JAXB access methods (public member, field, property, etc.), EclipseLink MOXy 2.3 introduces the concept of "virtual access methods", which instead rely on special '''get()''' and '''set()''' methods to maintain mapping data. For example, you might want to use a '''HashMap''' as the underlying data structure to hold all of your mapping data. The mappings that use virtual method access must be defined in EclipseLink OXM metadata. |
− | == Example | + | == Example == |
For this example we will use the following '''Employee''' class. In addition to some conventional JAXB mappings, we also specify that this class contains virtual access methods by including the '''@XmlVirtualAccessMethods''' annotation. | For this example we will use the following '''Employee''' class. In addition to some conventional JAXB mappings, we also specify that this class contains virtual access methods by including the '''@XmlVirtualAccessMethods''' annotation. | ||
+ | |||
+ | === Using Annotations === | ||
<source lang="java"> | <source lang="java"> | ||
Line 43: | Line 45: | ||
} | } | ||
</source> | </source> | ||
+ | |||
+ | === Using EclipseLink OXM === | ||
+ | |||
+ | '''@XmlVirtualAccessMethods''' can be specified in OXM as follows: | ||
+ | |||
+ | Next, we define our virtual mappings in an EclipseLink OXM file. Any property encountered in this file that does not have a corresponding Java attribute will be considered a virtual property and will be accessed through the virtual access methods. | ||
Revision as of 16:34, 20 June 2011
EclipseLink MOXy
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Key API
Contents
Virtual Access Methods
In addition to the standard JAXB access methods (public member, field, property, etc.), EclipseLink MOXy 2.3 introduces the concept of "virtual access methods", which instead rely on special get() and set() methods to maintain mapping data. For example, you might want to use a HashMap as the underlying data structure to hold all of your mapping data. The mappings that use virtual method access must be defined in EclipseLink OXM metadata.
Example
For this example we will use the following Employee class. In addition to some conventional JAXB mappings, we also specify that this class contains virtual access methods by including the @XmlVirtualAccessMethods annotation.
Using Annotations
@XmlRootElement @XmlVirtualAccessMethods @XmlAccessorType(AccessType.PROPERTY) public class Customer { private int id; private String name; private Map<String, Object> extensions = new HashMap<String, Object>(); public Object get(String name) { return extensions.get(name); } public void set(String name, Object value) { extensions.put(name, value); } @XmlAttribute public int getId() { ... }
Using EclipseLink OXM
@XmlVirtualAccessMethods can be specified in OXM as follows:
Next, we define our virtual mappings in an EclipseLink OXM file. Any property encountered in this file that does not have a corresponding Java attribute will be considered a virtual property and will be accessed through the virtual access methods.
XmlAccessorType and XmlTransient
If you are using an @XmlAccessorType other than AccessType.PROPERTY, you will need to mark your virtual properties Map attribute to be @XmlTransient, to prevent the Map itself from being bound to XML.
@XmlRootElement @XmlVirtualAccessMethods @XmlAccessorType(AccessType.FIELD) public class Customer { @XmlTransient private Map<String, Object> extensions; ...