Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "User:Rick.barkhouse.oracle.com/Test1"

(Removing all content from page)
 
(344 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide
 
|eclipselink=y
 
|eclipselinktype=MOXy
 
|info=y
 
|api=y
 
|apis= * [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/oxm/annotations/XmlVirtualAccessMethods.html XmlVirtualAccessMethods]
 
|toc=y
 
}}
 
  
= 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 properties and 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 structure to hold data for certain mappings.  The mappings that use virtual method access must be defined in EclipseLink OXM metadata.
 
 
In order to add virtual properties to an entity:
 
 
* the Java class must be marked with an <tt>@XmlVirtualAccessMethods</tt> annotation, or <tt><xml-virtual-access-methods></tt> element in OXM
 
* the Java class must contain getter and setter methods to access virtual property values
 
** <tt>public Object get(String propertyName)</tt>
 
** <tt>public void set(String propertyName, Object value)</tt>
 
** method names are configurable but must have the same method signatures as above
 
 
 
<div style="width:800px">
 
{{tip||By default, EclipseLink will look for methods named "set" and "get".  To customize accessor method names, see [[#Specifying Alternate Accessor Methods|Specifying Alternate Accessor Methods]].}}
 
</div>
 
 
 
== 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 ===
 
 
<source lang="java">
 
@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() {
 
  ...
 
 
}
 
</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.
 
 
 
== XmlAccessorType and XmlTransient ==
 
 
If you are using an <tt>@XmlAccessorType</tt> other than <tt>AccessType.PROPERTY</tt>, you will need to mark your virtual properties Map attribute to be <tt>@XmlTransient</tt>, to prevent the Map itself from being bound to XML.
 
 
<source lang="java">
 
@XmlRootElement
 
@XmlVirtualAccessMethods
 
@XmlAccessorType(AccessType.FIELD)
 
public class Customer {
 
 
  @XmlTransient
 
  private Map<String, Object> extensions;
 
  ...
 
</source>
 

Latest revision as of 16:29, 3 June 2013

Back to the top