Jump to: navigation, search

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

Line 1: Line 1:
 
{{EclipseLink_UserGuide
 
{{EclipseLink_UserGuide
 
|info=y
 
|info=y
 +
|toc=y
 
|exampes=y
 
|exampes=y
 
|example=* [[EclipseLink/Examples/MOXy/JPA/Relationships#One_To_Many|One-to-many]]
 
|example=* [[EclipseLink/Examples/MOXy/JPA/Relationships#One_To_Many|One-to-many]]
Line 84: Line 85:
 
</source>
 
</source>
  
==Using EclipseLink's @XmlPath Annotation==
+
==Grouping Elements using EclipseLink's @XmlPath Annotation==
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:
+
To make the elements of the '''Collection''' appear inside a grouping element, you can use '''@XmlPath''':
  
 
<source lang="java">
 
<source lang="java">
Line 91: Line 92:
 
@XmlAccessorType(XmlAccessType.FIELD)
 
@XmlAccessorType(XmlAccessType.FIELD)
 
public class Customer {
 
public class Customer {
   @XmlPath("contact-info/phone-number")
+
   @XmlPath("phone-numbers/phone-number")
 
   private PhoneNumber phoneNumber;
 
   private PhoneNumber phoneNumber;
  
Line 102: Line 103:
 
<source lang="xml">
 
<source lang="xml">
 
<customer>
 
<customer>
   <contact-info>
+
   <first-name>Bob</first-name>
       <phone-number>
+
  <last-name>Smith</last-name>
         <number>555-631-2124</number>
+
  <phone-numbers>
 +
       <phone-number type="Home">
 +
         <number>5559827222</number>
 
       </phone-number>
 
       </phone-number>
   </contact-info>
+
      <phone-number type="Work">
 +
        <number>5558872216</number>
 +
      </phone-number>
 +
   </phone-numbers>
 
</customer>
 
</customer>
 
</source>
 
</source>
 
You can also use '''@XmlPath''' to map to different occurrences of the same element in XML, by index.  For example:
 
 
<source lang="java">
 
@XmlRootElement
 
@XmlAccessorType(XmlAccessType.FIELD)
 
public class Customer {
 
  @XmlPath("contact-info/phone[1]")
 
  private PhoneNumber homePhone;
 
  @XmlPath("contact-info/phone[2]")
 
  private PhoneNumber workPhone;
 
  ...
 
}
 
</source> 
 
 
will produce the following XML:
 
 
<source lang="xml">
 
<customer>
 
  <contact-info>
 
      <phone>
 
        <number>555-631-2124</number>
 
      </phone>
 
      <phone>
 
        <number>555-631-8298</number>
 
      </phone>
 
  </contact-info>
 
</customer>
 
</source>
 
 
  
 
{{EclipseLink_MOXy
 
{{EclipseLink_MOXy

Revision as of 16:25, 10 March 2011


link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug 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:sequence>
         <xsd:element name="first-name" type="xsd:string"/>
         <xsd:element name="last-name" type="xsd:string"/>
         <xsd:element name="phone-number" type="phone-type" minOccurs="0" maxOccurs="unbounded"/>
      </xsd:sequence>
   </xsd:complexType>
 
   <xsd:complexType name="phone-type">
      <xsd:sequence>
         <xsd:attribute name="type" type="xsd:string"/>
         <xsd:element name="number" type="xsd:int"/>
      </xsd:sequence>
   </xsd:complexType>
 
</xsd:schema>

XML Composite Collection Mapping

The following example shows how to annotate your Java class to obtain this mapping with EclipseLink. The standard JAXB @XmlElement annotation, when used on a Collection field, can achieve this.

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
   @XmlElement(name="first-name")
   private String firstName;
   @XmlElement(name="last-name")
   private String lastName;
   @XmlElement(name="phone-number")
   private List<PhoneNumber> phoneNumbers;
 
   ...
}
 
@XmlAccessorType(XmlAccessType.FIELD)
public class PhoneNumber {
   private String type;
   private Integer number;
 
   ...
}

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

...
<java-type name="Customer">
   <xml-root-element name="customer"/>
   <java-attributes>
      <xml-element java-attribute="firstName" name="phone-number" type="java.lang.String"/>
      <xml-element java-attribute="lastName" name="phone-number" type="java.lang.String"/>
      <xml-element java-attribute="phoneNumbers" name="phone-number" type="PhoneNumber"/>
   </java-attributes>
</java-type>
 
<java-type name="PhoneNumber">
   <java-attributes>
      <xml-attribute java-attribute="type" type="java.lang.String"/>
      <xml-value java-attribute="number" type="java.lang.Integer"/>
   </java-attributes>
</java-type>
...

Grouping Elements using EclipseLink's @XmlPath Annotation

To make the elements of the Collection appear inside a grouping element, you can use @XmlPath:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
   @XmlPath("phone-numbers/phone-number")
   private PhoneNumber phoneNumber;
 
   ...
}

This will produce the following XML:

<customer>
   <first-name>Bob</first-name>
   <last-name>Smith</last-name>
   <phone-numbers>
      <phone-number type="Home">
         <number>5559827222</number>
      </phone-number>
      <phone-number type="Work">
         <number>5558872216</number>
      </phone-number>
   </phone-numbers>
</customer>

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