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 "EclipseLink/UserGuide/MOXy/Relationships/Privately Owned/One-to-One"

(Using EclipseLink's @XmlPath Annotation)
m (Replacing page with 'Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/privately_owned_relationships001.htm')
 
(22 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide
+
Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/privately_owned_relationships001.htm
|eclipselink=y
+
|eclipselinktype=MOXy
+
|info=y
+
|toc=y
+
|api=y
+
|apis= * [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/XmlElement.html javax.xml.bind.annotation.XmlElement]
+
}}
+
 
+
=Mapping Privately-Owned One-to-One Relationships=
+
This section demonstrates several ways to map a one-to-one relationship between objects.  By default, one-to-one relationships are privately-owned in JAXB -- their XML content will appear nested inside the owning element.  For example, a '''Customer''' with a one-to-one mapping to a '''PhoneNumber''' would be marshalled as:
+
 
+
<source lang="xml">
+
<customer>
+
  <name>Bob Smith</name>
+
  <id>1982812</id>
+
  <phone-number>
+
      <area-code>613</area-code>
+
      <number>5550210</number>
+
      <extension>20016</extension>
+
  </phone-number>
+
</customer>
+
</source>
+
 
+
==Mapping to an Element==
+
Given the XML schema in this example, the figure below illustrates a one-to-one relationship between two complex types.
+
 
+
<source lang="xml">
+
<?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="phone-number" type="phone-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>
+
</source>
+
 
+
[[Image:onetoone.png]]<br><br>
+
 
+
The following example shows how to annotate your Java class to obtain this mapping with EclipseLink.  The standard JAXB '''@XmlElement''' annotation can be used to indicate that the associated Java field should be mapped to an XML element. 
+
 
+
{{tip||By default, JAXB will assume all fields on your Java object are '''@XmlElement'''s, so in many cases the annotation itself is not required.  If, however, you want to customize the Java field's XML name, you can specify an '''@XmlElement''' annotation with a '''name''' argument.}}
+
 
+
<source lang="java">
+
@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;
+
 
+
  ...
+
}
+
</source>
+
 
+
The following example shows how to define your mapping information in EclipseLink's OXM metadata format.
+
 
+
<source lang="xml">
+
...
+
<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>
+
...
+
</source>
+
 
+
==Using EclipseLink's @XmlPath Annotation==
+
By default, your Java attributes will be mapped to XML based on their attributes Java name, or by a name specified in an '''@XmlElement''' annotation.  This mapping is based on XPath, and EclipseLink's '''@XmlPath''' annotation allows you to customize this mapping.  For example, you can use it to control the nesting of your elements in XML:
+
 
+
<source lang="java">
+
@XmlRootElement
+
@XmlAccessorType(XmlAccessType.FIELD)
+
public class Customer {
+
  @XmlPath("contact-info/phone-number")
+
  private PhoneNumber phoneNumber;
+
 
+
  ...
+
}
+
</source> 
+
 
+
This will produce the following XML:
+
 
+
<source lang="xml">
+
<customer>
+
  <contact-info>
+
      <phone-number>
+
        <number>555-631-2124</number>
+
      </phone-number>
+
  </contact-info>
+
</customer>
+
</source>
+
 
+
You can also use '''@XmlPath''' to map to different occurrences of the same element in XML, by index.  For example:
+
 
+
<source lang="java">
+
@XmlRootElement
+
@XmlAccessorType(XmlAccessType.FIELD)
+
public class Customer {
+
  @XmlPath("contact-info/phone[1]")
+
  private PhoneNumber homePhone;
+
  @XmlPath("contact-info/phone[2]")
+
  private PhoneNumber workPhone;
+
  ...
+
}
+
</source> 
+
 
+
This will produce the following XML:
+
 
+
<source lang="xml">
+
<customer>
+
  <contact-info>
+
      <phone>
+
        <number>555-631-2124</number>
+
      </phone>
+
      <phone>
+
        <number>555-631-8298</number>
+
      </phone>
+
  </contact-info>
+
</customer>
+
</source>
+
 
+
=="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.
+
 
+
<source lang="java">
+
@XmlRootElement
+
@XmlAccessorType(XmlAccessType.FIELD)
+
public class Customer {
+
  @XmlPath(".")
+
  private PhoneNumber phoneNumber;
+
 
+
  ...
+
}
+
 
+
public class PhoneNumber {
+
  ...
+
}
+
</source>
+
 
+
The following below shows a self mapping defined in EclipseLink's OXM metadata format.
+
 
+
<source lang="xml">
+
...
+
<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>
+
...
+
</source>
+
 
+
Using a self mapping, EclipseLink produces the following XML:
+
 
+
<source lang="xml">
+
<customer>
+
  <area-code>613</area-code>
+
  <number>2883000</number>
+
  <extension>1547</extension>
+
</customer>
+
</source>
+
 
+
 
+
{{EclipseLink_MOXy
+
|version=2.2.0 DRAFT
+
|previous=[[EclipseLink/UserGuide/MOXy/Relationships/Privately Owned|Privately owned]]
+
|next=[[EclipseLink/UserGuide/MOXy/Relationships/Privately_Owned/One-to-Many|One-to-many]]
+
|up=[[EclipseLink/UserGuide/MOXy/Relationships/Privately Owned|Privately owned]]
+
}}
+

Latest revision as of 10:21, 8 November 2012

Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/privately_owned_relationships001.htm

Back to the top