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:

+

−

+

−

<div style="width:700px">

+

−

<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>

+

−

</div>

+

−

+

−

== Mapping to an Element ==

+

−

+

−

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

+

−

+

−

<div style="width:700px">

+

−

<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>

+

−

</div>

+

−

+

−

[[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.}}

+

−

+

−

<div style="width:700px">

+

−

<source lang="java">

+

−

package example;

+

−

+

−

import javax.xml.bind.annotation.*;

+

−

+

−

@XmlRootElement

+

−

@XmlAccessorType(XmlAccessType.FIELD)

+

−

public class Customer {

+

−

@XmlElement(name="phone-number")

+

−

private PhoneNumber phoneNumber;

+

−

+

−

...

+

−

}

+

−

</source>

+

−

</div>

+

−

+

−

<div style="width:700px">

+

−

<source lang="java">

+

−

package example;

+

−

+

−

import javax.xml.bind.annotation.*;

+

−

+

−

@XmlAccessorType(XmlAccessType.FIELD)

+

−

public class PhoneNumber {

+

−

@XmlElement(name="area-code")

+

−

private Integer areaCode;

+

−

+

−

private Integer number;

+

−

+

−

private Integer extension;

+

−

+

−

...

+

−

}

+

−

</source>

+

−

</div>

+

−

+

−

The following example shows how to define your mapping information in an EclipseLink's XML Bindings document.

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: