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.
Difference between revisions of "EclipseLink/UserGuide/MOXy/Relationships/Privately Owned/One-to-Many"
< EclipseLink | UserGuide | MOXy | Relationships | Privately Owned
Line 27: | Line 27: | ||
<xsd:complexType name="phone-type"> | <xsd:complexType name="phone-type"> | ||
<xsd:sequence> | <xsd:sequence> | ||
− | <xsd: | + | <xsd:attribute name="type" type="xsd:string"/> |
<xsd:element name="number" type="xsd:int"/> | <xsd:element name="number" type="xsd:int"/> | ||
</xsd:sequence> | </xsd:sequence> |
Revision as of 14:59, 10 March 2011
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
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>
With Java
Java for XML Composite Collection Mapping for a Collection AttributeXMLCompositeCollectionMapping phoneNumbersMapping = new XMLCompositeCollectionMapping(); phoneNumbersMapping.setAttributeName("phoneNumbers"); phoneNumbersMapping.setXPath("phone-number"); phoneNumbersMapping.setReferenceClass(PhoneNumber.class);
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; }