Jump to: navigation, search

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

Line 38: Line 38:
  
 
The following example shows how to annotate your Java class to obtain this mapping with EclipseLink.  The standard JAXB '''@XmlElement''' annotation can be used to indicate that the associated Java field should be mapped to an XML element.   
 
The following example shows how to annotate your Java class to obtain this mapping with EclipseLink.  The standard JAXB '''@XmlElement''' annotation can be used to indicate that the associated Java field should be mapped to an XML element.   
 
{{tip||By default, JAXB will assume all fields on your Java object are '''@XmlElement'''s, so in many cases the annotation itself is not required.  If, however, you want to customize the Java field's XML name, you can specify an '''@XmlElement''' annotation with a '''name''' argument.}}
 
  
 
<source lang="java">
 
<source lang="java">
Line 50: Line 48:
 
   private String lastName;
 
   private String lastName;
 
   @XmlElement(name="phone-number")
 
   @XmlElement(name="phone-number")
   private PhoneNumber phoneNumber;
+
   private List<PhoneNumber> phoneNumbers;
  
 
   ...
 
   ...
Line 79: Line 77:
 
<java-type name="PhoneNumber">
 
<java-type name="PhoneNumber">
 
   <java-attributes>
 
   <java-attributes>
       <xml-value java-attribute="type" type="java.lang.String"/>
+
       <xml-attribute java-attribute="type" type="java.lang.String"/>
 
       <xml-value java-attribute="number" type="java.lang.Integer"/>
 
       <xml-value java-attribute="number" type="java.lang.Integer"/>
 
   </java-attributes>
 
   </java-attributes>

Revision as of 15:04, 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 can be used to indicate that the associated Java field should be mapped to an XML element.

@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>
...

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