Jump to: navigation, search

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

m (Replacing page with 'See http://www.eclipse.org/eclipselink/documentation/2.4/moxy/privately_owned_relationships002.htm')
 
(27 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide
+
See http://www.eclipse.org/eclipselink/documentation/2.4/moxy/privately_owned_relationships002.htm
|info=y
+
|exampes=y
+
|example=* [[EclipseLink/Examples/MOXy/JPA/Relationships#One_To_Many|One-to-many]]
+
}}
+
 
+
= One-to-Many Mapping =
+
 
+
This section illustrates how to map one-to-many relationships with Eclipselink.
+
 
+
The schema below shows a typical 1:M relationship between '''Customer''' and '''PhoneNumber'''
+
 
+
<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-numbers" type="phone-type" minOccurs="0" maxOccurs="unbounded"/>
+
  </xsd:complexType>
+
 
+
  <xsd:complexType name="phone-type">
+
      <xsd:element name="type" type="xsd:string"/>
+
      <xsd:element name="number" type="xsd:int"/>
+
  </xsd:complexType>
+
 
+
</xsd:schema>
+
</source>
+
 
+
[[Image:Ccxm.gif|XML Composite Collection Mapping]]<br><br>
+
 
+
 
+
== With Java==
+
'''''Java for XML Composite Collection Mapping for a Collection Attribute''''' <source lang="java">
+
XMLCompositeCollectionMapping phoneNumbersMapping = new XMLCompositeCollectionMapping();
+
phoneNumbersMapping.setAttributeName("phoneNumbers");
+
phoneNumbersMapping.setXPath("phone-number");
+
phoneNumbersMapping.setReferenceClass(PhoneNumber.class);
+
</source>
+
 
+
'''''Java for XML Composite Collection Mapping for a Map Attribute''''' <source lang="java">
+
XMLCompositeCollectionMapping phoneNumbersMapping = new XMLCompositeCollectionMapping();
+
phoneNumbersMapping.setAttributeName("phoneNumbers");         
+
phoneNumbersMapping.setXPath("phone-number");
+
phoneNumbersMapping.setReferenceClass(PhoneNumber.class);
+
phoneNumbersMapping.useMapClass(HashMap.class, "getType");
+
</source>
+
 
+
== With EclipseLink Annotations ==
+
Use the '''@OneToMany''' annotation:
+
<source lang="java">
+
@Entity
+
public class Employee {
+
+
    @OneToMany(mappedBy="contact")
+
    private List<PhoneNumber> contactNumber;
+
+
}
+
</source>
+
 
+
Use the EclipseLink extension '''@XmlInverseReference''' to map the back-pointer:
+
<source lang="java">
+
@Entity
+
public class PhoneNumber {
+
+
    @ManyToOne
+
    @JoinColumn(name="E_ID", referencedColumnName = "E_ID")
+
    @XmlInverseReference(mappedBy="contactNumber")
+
    private Employee contact;
+
+
}
+
</source>
+
{{EclipseLink_MOXy
+
|version=2.2.0 DRAFT
+
|previous=[[EclipseLink/UserGuide/MOXy/Relationships/Privately_Owned/One-to-One|One-to-one]]
+
|next=[[EclipseLink/UserGuide/MOXy/Relationships/Shared_Reference|Shared references]]
+
|up=[[EclipseLink/UserGuide/MOXy/Relationships/Privately Owned|Privately owned]]
+
}}
+

Latest revision as of 09:22, 8 November 2012

See http://www.eclipse.org/eclipselink/documentation/2.4/moxy/privately_owned_relationships002.htm