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"

Line 5: Line 5:
 
}}
 
}}
  
=Mapping Collections of Simple Values=
+
=Mapping One-to-One Relationships=
This section demonstrates several ways to map a collection of simple Java values directly to XML text nodes.
+
This section demonstrates several ways to map a one-to-one relationship between objects.
  
 
==Mapping to an Attribute==
 
==Mapping to an Attribute==

Revision as of 16:40, 22 December 2010


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

Elug example icon.png Examples


Mapping One-to-One Relationships

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

Mapping to an Attribute

Given the XML schema in this example, the figure below illustrates an XML direct mapping to an attribute in a corresponding XML document.

<?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:attribute name="id" type="xsd:integer"/>
   </xsd:complexType>
 
</xsd:schema>

XML Direct Mapping to an Attribute

The following example shows how to annotate your Java class to obtain this mapping with EclipseLink. All that is needed is the standard JAXB @XmlAttribute annotation.

@XmlRootElement
public class Customer {
   @XmlAttribute
   public Integer id;
}

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

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



One-to-One Composite Collection Mapping

With EclipseLink Annotations

Use the @OneToOne annotation:

@Entity
public class Employee {
 
    @OneToOne(mappedBy="resident")
    private Address residence;
 
}

Use the EclipseLink extension @XmlInverseReference to map the back-pointer:

@Entity
public class Address {
 
    @OneToOne
    @JoinColumn(name="E_ID")
    @XmlInverseReference(mappedBy="residence")
    private Employee resident;
 
}

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

Back to the top