Skip to main content

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.

Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/MOXy/Relationships/Privately Owned/One-to-One"

m
m
Line 4: Line 4:
 
|info=y
 
|info=y
 
|toc=y
 
|toc=y
 +
|api=y
 +
|apis= * [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/XmlElement.html javax.xml.bind.annotation.XmlElement]
 
}}
 
}}
  
Line 33: Line 35:
 
[[Image:onetoone.png]]<br><br>
 
[[Image:onetoone.png]]<br><br>
  
The following example shows how to annotate your Java class to obtain this mapping with EclipseLink. All that is needed is the standard JAXB <tt>@XmlElement</tt> annotation.
+
The following example shows how to annotate your Java class to obtain this mapping with EclipseLink. Use the the standard JAXB '''@XmlElement''' annotation.
  
 
<source lang="java">
 
<source lang="java">
Line 58: Line 60:
 
</source>
 
</source>
  
The example below shows how to define your mapping information in EclipseLink's OXM metadata format.
+
The following example shows how to define your mapping information in EclipseLink's OXM metadata format.
  
 
<source lang="xml">
 
<source lang="xml">
Line 78: Line 80:
 
...
 
...
 
</source>
 
</source>
 +
  
 
=="Self" Mappings==
 
=="Self" Mappings==
 +
EclipseLink allows you to configure your one-to-one mapping so the data from the target object will appear inside the source object's XML element. Using the previous example, the '''PhoneNumber''' information would appear directly under the '''customer''' element, and '''''not''''' wrapped in a '''phone-number''' element.  This is referred to as a "self" mapping, and is achieved by setting the target object's XPath to '''.''' (dot). 
  
EclipseLink allows you to configure your one-to-one mapping such that the data from the target object will appear inside the source object's XML element.  Using the example above, this means that the "<tt>PhoneNumber</tt>" information would appear directly under the "<tt>customer</tt>" element, and not wrapped in a "<tt>phone-number</tt>" element.  This is referred to as a "self" mapping, and is achieved by setting the target object's XPath to "'''.'''" (dot).  The following example demonstrates a self mapping declared in annotations.
+
The following example demonstrates a self mapping declared in annotations.
  
 
<source lang="java">
 
<source lang="java">
Line 98: Line 102:
 
</source>
 
</source>
  
The example below shows a self mapping defined on EclipseLink OXM metadata format.
+
The following below shows a self mapping defined in EclipseLink's OXM metadata format.
  
 
<source lang="xml">
 
<source lang="xml">
Line 115: Line 119:
 
</source>
 
</source>
  
Using a self mapping, the following XML will be produced:
+
Using a self mapping, EclipseLink produces the following XML:
  
 
<source lang="xml">
 
<source lang="xml">

Revision as of 10:55, 10 January 2011

EclipseLink MOXy

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source

Mapping One-to-One Relationships

This section demonstrates several ways to map a one-to-one relationship between objects.

Mapping to a Complex Type

Given the XML schema in this example, the figure below illustrates a one-to-one relationship between two complex types.

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 
   <xsd:element name="customer" type="customer-type"/>
 
   <xsd:complexType name="customer-type">
      <xsd:element name="address" type="address-type"/>
   </xsd:complexType>
 
   <xsd:complexType name="phone-type">
      <xsd:element name="area-code" type="xsd:int"/>
      <xsd:element name="number" type="xsd:int"/>
      <xsd:element name="extension" type="xsd:int"/>
   </xsd:complexType>
 
</xsd:schema>

Onetoone.png

The following example shows how to annotate your Java class to obtain this mapping with EclipseLink. Use the the standard JAXB @XmlElement annotation.

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
   @XmlElement(name="phone-number")
   private PhoneNumber phoneNumber;
 
   ...
}
 
@XmlAccessorType(XmlAccessType.FIELD)
public class PhoneNumber {
   @XmlElement(name="area-code")
   private Integer areaCode;
 
   private Integer number;
 
   private Integer extension;
 
   ...
}

The following example shows how to define your mapping information in EclipseLink's OXM metadata format.

...
<java-type name="Customer">
   <xml-root-element name="customer"/>
   <java-attributes>
      <xml-element java-attribute="phoneNumber" name="phone-number" type="PhoneNumber"/>
   </java-attributes>
</java-type>
 
<java-type name="PhoneNumber">
   <java-attributes>
      <xml-value java-attribute="areaCode" name="area-code" type="java.lang.Integer"/>
      <xml-value java-attribute="number" type="java.lang.Integer"/>
      <xml-value java-attribute="extension" type="java.lang.Integer"/>
   </java-attributes>
</java-type>
...


"Self" Mappings

EclipseLink allows you to configure your one-to-one mapping so the data from the target object will appear inside the source object's XML element. Using the previous example, the PhoneNumber information would appear directly under the customer element, and not wrapped in a phone-number element. This is referred to as a "self" mapping, and is achieved by setting the target object's XPath to . (dot).

The following example demonstrates a self mapping declared in annotations.

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
   @XmlPath(".")
   private PhoneNumber phoneNumber;
 
   ...
}
 
public class PhoneNumber {
   ...
}

The following below shows a self mapping defined in EclipseLink's OXM metadata format.

...
<java-type name="Customer">
   <xml-root-element name="customer"/>
   <java-attributes>
      <xml-element java-attribute="phoneNumber" type="PhoneNumber" xml-path="."/>
   </java-attributes>
</java-type>
 
<java-type name="PhoneNumber">
   ...
</java-type>
...

Using a self mapping, EclipseLink produces the following XML:

<customer>
   <area-code>613</area-code>
   <number>2883000</number>
   <extension>1547</extension>
</customer>


Eclipselink-logo.gif
Version: 2.2.0 DRAFT
Other versions...

Back to the top