Skip to main content

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.

Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/MOXy/Relationships/Bidirectional Relationships"

m (Replacing page with 'See http://www.eclipse.org/eclipselink/documentation/2.4/moxy/shared_reference_relationships005.htm')
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide
+
See http://www.eclipse.org/eclipselink/documentation/2.4/moxy/shared_reference_relationships005.htm
|info=y
+
|api=y
+
|apis= * [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/oxm/annotations/XmlInverseReference.html XmlInverseReference]
+
}}
+
 
+
= Bidirectional Relationships  =
+
 
+
In order to map bidirectional relationships in EclipseLink MOXy, the back-pointer must be annotated as an '''@XmlInverseReference'''.  Without this annotation, the cyclic relationship will result in an infinite loop during marshalling.
+
 
+
'''@XmlInverseReferences''' must specify the '''mappedBy''' attribute, which indicates the property on the opposite side of the relationship.
+
 
+
 
+
In this example, an '''Employee''' has a collection of '''PhoneNumbers''', and each '''PhoneNumber''' has a back-pointer back to its '''Employee''':
+
 
+
<source lang="java">
+
@XmlAccessorType(XmlAccessType.FIELD)
+
public class Employee {
+
  private String name;
+
  private List<PhoneNumber> phones = new ArrayList<PhoneNumber>();
+
  ...
+
}
+
 
+
@XmlAccessorType(XmlAccessType.FIELD)
+
public class PhoneNumber {
+
  private String number;
+
  @XmlInverseReference(mappedBy="phones")
+
  private Employee employee;
+
  ...
+
}
+
</source>
+
 
+
 
+
The following example shows how to define this mapping in EclipseLink's OXM metadata format:
+
 
+
<source lang="xml">
+
...
+
<java-type name="Employee">
+
  <java-attributes>
+
      <xml-element java-attribute="name" type="java.lang.String"/>
+
      <xml-element java-attribute="phones" type="PhoneNumber" container-type="java.util.ArrayList"/>
+
  </java-attributes>
+
</java-type>
+
 
+
<java-type name="PhoneNumber">
+
  <java-attributes>
+
      <xml-value java-attribute="areaCode" name="area-code" type="java.lang.Integer"/>
+
      <xml-value java-attribute="number" type="java.lang.Integer"/>
+
      <xml-value java-attribute="extension" type="java.lang.Integer"/>
+
  </java-attributes>
+
</java-type>
+
...
+
</source>
+
 
+
 
+
In addition, when using '''@XmlInverseReference''', it is not necessary to explicitly set the back-pointer in your Java code; EclipseLink will do this for you automatically:
+
 
+
<source lang="java">
+
Employee emp = new Employee();
+
emp.setName("Bob Smith");
+
 
+
PhoneNumber p = new PhoneNumber();
+
p.setNumber("555-1212");
+
 
+
emp.getPhones().add(p);
+
 
+
// Not Necessary
+
// p.setEmployee(emp);
+
</source>
+
 
+
 
+
'''@XmlInverseReference''' back-pointers can be used with the following types of mappings:
+
 
+
* [[EclipseLink/UserGuide/MOXy/Relationships/Privately_Owned/One-to-One|One-To-One Relationships]]
+
* [[EclipseLink/UserGuide/MOXy/Relationships/Privately_Owned/One-to-Many|One-To-Many Relationships]]
+
* [[EclipseLink/UserGuide/MOXy/Relationships/Shared_Reference/Keys_and_Foreign_Keys/Single_Key|Single Key Relationships]]
+
* [[EclipseLink/UserGuide/MOXy/Relationships/Shared_Reference/Keys_and_Foreign_Keys/Composite_Key|Composite Key Relationships]]
+
 
+
 
+
'''@XmlInverseReference''' can be particularly useful when mapping JPA entities to XML.  For an example, please see [[EclipseLink/Examples/MOXy/JPA/Relationships|Binding JPA Relationships to XML]].
+
 
+
 
+
{{EclipseLink_MOXy
+
|previous=[[EclipseLink/UserGuide/MOXy/Relationships/Collections and Maps|Collections and Maps]]
+
|up      =[[EclipseLink/UserGuide/MOXy/Relationships|Relationships]]
+
|next    =[[EclipseLink/UserGuide/MOXy/Advanced XML Schema Concepts|Advanced XML Schema Concepts]]
+
|version=2.2.0 DRAFT
+
}}
+

Latest revision as of 10:28, 8 November 2012

See http://www.eclipse.org/eclipselink/documentation/2.4/moxy/shared_reference_relationships005.htm

Back to the top