Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/MOXy/Mapping JPA Entities to XML/Bidirectional Relationships"

m
m (Replacing page with ''''Warning This page is obsolete. Please see ''[http://www.eclipse.org/eclipselink/documentation/ Developing JAXB Applications Using EclipseLink M...')
 
Line 1: Line 1:
{{EclipseLink_UserGuide
+
'''[[Image:Elug_draft_icon.png|Warning]] This page is obsolete. Please see ''[http://www.eclipse.org/eclipselink/documentation/ Developing JAXB Applications Using EclipseLink MOXy]'' for current information.'''
|info=y
+
|toc=y
+
|eclipselink=y
+
|eclipselinktype=MOXy
+
|api=y
+
|apis= * [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/XmlInverseReference.html XmlInverseReference]
+
}}
+
 
+
==Relationships - Privately Owned==
+
These relationships apply when the target object(s) is referenced by only a single source object. This type of relationship can be safely represented as ''nesting'' in XML (the default in JAXB).
+
 
+
===One To One and Embedded===
+
In JPA, the '''OneToOne''' and '''Embedded''' annotations indicate only ''one'' instance of the source entity is able to refer to the same target entity instance. Since the relationship between '''Employee''' and '''Address''' is bi-directional, use the EclipseLink extension '''@XmlInverseReference''' to represent the back-pointer.
+
 
+
<source lang="java">
+
@Entity
+
public class Employee {
+
+
    @OneToOne(mappedBy="resident")
+
    private Address residence;
+
+
}
+
 
+
@Entity
+
public class Address {
+
+
    @OneToOne
+
    @JoinColumn(name="E_ID")
+
    @XmlInverseReference(mappedBy="residence")
+
    private Employee resident;
+
+
}
+
</source>
+
 
+
===One To Many===
+
In JPA, the '''OneToMany''' annotation indicates that only one instance of the source entity can refer to the same target entity instance. Since the relationship between '''Employee''' and '''Address''' is bi-directional, use the EclipseLink extension '''@XmlInverseReference''' to map the back-pointer.
+
 
+
<source lang="java">
+
@Entity
+
public class Employee {
+
+
    @OneToMany(mappedBy="contact")
+
    private List<PhoneNumber> contactNumber;
+
+
}
+
 
+
@Entity
+
public class PhoneNumber {
+
+
    @ManyToOne
+
    @JoinColumn(name="E_ID", referencedColumnName = "E_ID")
+
    @XmlInverseReference(mappedBy="contactNumber")
+
    private Employee contact;
+
+
}
+
 
+
</source>
+
 
+
 
+
 
+
{{EclipseLink_MOXy
+
|previous=    [[EclipseLink/UserGuide/MOXy/Mapping_JPA_Entities_to_XML/Lazily_Loaded_Fields/Properties|Properties]]
+
|next= [[EclipseLink/UserGuide/MOXy/Mapping_JPA_Entities_to_XML/Keys_and_Foreign_Keys|Keys and Foreign Keys]]
+
|up=      [[EclipseLink/UserGuide/MOXy/Mapping_JPA_Entities_to_XML|Mapping JPA Entities to XML]]
+
|version=2.2.0 DRAFT}}
+

Latest revision as of 13:13, 30 January 2013

Warning This page is obsolete. Please see Developing JAXB Applications Using EclipseLink MOXy for current information.