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

Line 9: Line 9:
 
This section illustrates how to map one-to-many relationships with Eclipselink.  
 
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">
  
Use XML composite collection mappings to represent one-to-many relationships. Composite collection XML mappings can reference any class that has an EclipseLink descriptor. The attribute in the object mapped must implement either the Java <tt>Collection</tt> interface (for example, <tt>Vector</tt> or <tt>HashSet</tt>) or <tt>Map</tt> interface (for example, <tt>Hashtable</tt> or <tt>TreeMap</tt>). The <tt>CompositeCollectionMapping</tt> class allows a reference to the mapped class and the indexing type for that class.
+
  <xsd:element name="customer" type="customer-type"/>
  
Given the XML schema in this example, [[#Figure_58-27|XML Composite Collection Mapping]] illustrates an XML composite collection mapping to different elements by position in a corresponding XML document. [[#Example_58-49|Java for XML Composite Collection Mapping for a Collection Attribute]] shows how to configure this mapping in Java for a <tt>Collection</tt> attribute and [[#Example_58-50|Java for XML Composite Collection Mapping for a Map Attribute]] shows how to configure this mapping in Java for a <tt>Map</tt> attribute.
+
  <xsd:complexType name="customer-type">
 +
      <xsd:element name="phone-numbers" type="phone-type" minOccurs="0" maxOccurs="unbounded"/>
 +
  </xsd:complexType>
  
<br> <span id="Example_58-48">
+
  <xsd:complexType name="phone-type">
''''' Schema for XML Composite Collection Mapping'''''
+
      <xsd:element name="area-code" type="xsd:int"/>
<source lang="xml">
+
      <xsd:element name="number" type="xsd:int"/>
<?xml version="1.0" encoding="UTF-8"?>
+
      <xsd:element name="extension" type="xsd:int"/>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
+
  </xsd:complexType>
    <xsd:element name="customer" type="customer-type"/>
+
    <xsd:complexType name="customer-type">
+
        <xsd:sequence>
+
+
            <xsd:element name="first-name" type="xsd:string"/>
+
            <xsd:element name="last-name" type="xsd:string"/>
+
            <xsd:element name="phone-number">
+
                <xsd:complexType>
+
                    <xsd:sequence>
+
                        <xsd:element name="number" type="xsd:string"/>
+
+
                    </xsd:sequence>
+
                    <xsd:attribute name="type" type="xsd:string"/>
+
                </xsd:complexType>
+
            </xsd:element>
+
        </xsd:sequence>
+
    </xsd:complexType>
+
+
</xsd:schema>
+
</source></span>
+
  
<span id="Figure_58-27" /> '''''XML Composite Collection Mapping'''''
+
</xsd:schema>
 +
</source>
  
 
[[Image:Ccxm.gif|XML Composite Collection Mapping]]<br><br>  
 
[[Image:Ccxm.gif|XML Composite Collection Mapping]]<br><br>  

Revision as of 13:37, 10 March 2011


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


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

<?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="area-code" type="xsd:int"/>
      <xsd:element name="number" type="xsd:int"/>
      <xsd:element name="extension" type="xsd:int"/>
   </xsd:complexType>
 
</xsd:schema>

XML Composite Collection Mapping


With Java

Java for XML Composite Collection Mapping for a Collection Attribute
 XMLCompositeCollectionMapping phoneNumbersMapping = new XMLCompositeCollectionMapping();
 phoneNumbersMapping.setAttributeName("phoneNumbers");
 phoneNumbersMapping.setXPath("phone-number");
 phoneNumbersMapping.setReferenceClass(PhoneNumber.class);
Java for XML Composite Collection Mapping for a Map Attribute
 XMLCompositeCollectionMapping phoneNumbersMapping = new XMLCompositeCollectionMapping();
 phoneNumbersMapping.setAttributeName("phoneNumbers");           
 phoneNumbersMapping.setXPath("phone-number");
 phoneNumbersMapping.setReferenceClass(PhoneNumber.class);
 phoneNumbersMapping.useMapClass(HashMap.class, "getType");

With EclipseLink Annotations

Use the @OneToMany annotation:

@Entity
public class Employee {
 
    @OneToMany(mappedBy="contact")
    private List<PhoneNumber> contactNumber;
 
}

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

@Entity
public class PhoneNumber {
 
    @ManyToOne
    @JoinColumn(name="E_ID", referencedColumnName = "E_ID")
    @XmlInverseReference(mappedBy="contactNumber")
    private Employee contact;
 
}

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

Back to the top