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"

m (Replacing page with 'Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/privately_owned_relationships001.htm')
 
(66 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
+
}}
+
 
+
=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.
+
 
+
<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="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>
+
</source>
+
 
+
[[Image:dxmatt.gif|XML Direct Mapping to an Attribute]]<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.
+
 
+
<source lang="java">
+
@XmlRootElement
+
public class Customer {
+
  @XmlElement
+
  public PhoneNumber phoneNumber;
+
}
+
 
+
@XmlRootElement(name="phone-number")
+
public class PhoneNumber {
+
  @XmlElement(name="area-code")
+
  public Integer areaCode;
+
  @XmlElement
+
  public Integer number;
+
  @XmlElement
+
  public Integer extension;
+
}
+
</source>
+
 
+
The example below shows how to 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">
+
  <xml-root-element name="phone-number"/>
+
  <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>
+
 
+
<br><br>
+
 
+
= One-to-One Composite Collection Mapping =
+
 
+
 
+
 
+
== With EclipseLink Annotations ==
+
Use the '''@OneToOne''' annotation:
+
<source lang="java">
+
@Entity
+
public class Employee {
+
+
    @OneToOne(mappedBy="resident")
+
    private Address residence;
+
+
}
+
</source>
+
 
+
Use the EclipseLink extension '''@XmlInverseReference''' to map the back-pointer:
+
<source lang="java">
+
@Entity
+
public class Address {
+
+
    @OneToOne
+
    @JoinColumn(name="E_ID")
+
    @XmlInverseReference(mappedBy="residence")
+
    private Employee resident;
+
+
}
+
</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