|
|
(5 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
| + | |
− | |toc=y
| + | |
− | |api=y
| + | |
− | |apis= * [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/XmlElement.html XmlElement]
| + | |
− | * [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/XmlElementWrapper.html XmlElementWrapper]
| + | |
− | }}
| + | |
− | | + | |
− | = Privately Owned One-to-Many Relationships =
| + | |
− | | + | |
− | 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: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>
| + | |
− | </source>
| + | |
− | | + | |
− | [[Image:Ccxm.gif|XML Composite Collection Mapping]]<br><br>
| + | |
− | | + | |
− | ==Mapping to Elements==
| + | |
− | | + | |
− | 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''' or '''array''' field, can achieve this.
| + | |
− | | + | |
− | <source lang="java">
| + | |
− | @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 {
| + | |
− | @XmlAttribute
| + | |
− | private String type;
| + | |
− | private Integer number;
| + | |
− | | + | |
− | ...
| + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | The following example shows how to define your mapping information in EclipseLink's OXM metadata format.
| + | |
− | | + | |
− | <source lang="xml">
| + | |
− | ...
| + | |
− | <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>
| + | |
− | ...
| + | |
− | </source>
| + | |
− | | + | |
− | ==Grouping Elements using the @XmlElementWrapper Annotation==
| + | |
− | To make the elements of the '''Collection''' appear inside a grouping element, you can use '''@XmlElementWrapper''':
| + | |
− | | + | |
− | <source lang="java">
| + | |
− | @XmlRootElement
| + | |
− | @XmlAccessorType(XmlAccessType.FIELD)
| + | |
− | public class Customer {
| + | |
− | @XmlElement(name = "phone-number")
| + | |
− | @XmlElementWrapper(name="phone-numbers")
| + | |
− | private PhoneNumber[] phoneNumbers;
| + | |
− | | + | |
− | ...
| + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | This will produce the following XML:
| + | |
− | | + | |
− | <source lang="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>
| + | |
− | </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]]
| + | |
− | }}
| + | |