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

From Eclipsepedia

Jump to: navigation, search
Line 10: Line 10:
  
 
'''@XmlInverseReferences''' must specify the '''mappedBy''' attribute, which indicates the property on the opposite side of the relationship.
 
'''@XmlInverseReferences''' must specify the '''mappedBy''' attribute, which indicates the property on the opposite side of the relationship.
 
 
The following example shows how to define this mapping in EclipseLink's OXM metadata format:
 
 
<source lang="xml">
 
...
 
<java-type name="Customer">
 
  <xml-root-element name="customer"/>
 
  <java-attributes>
 
      <xml-element java-attribute="phoneNumber" name="phone-number" type="PhoneNumber"/>
 
  </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>
 
  
  
Line 52: Line 30:
 
}
 
}
 
</source>
 
</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="name" type="java.lang.String"/>
 +
 +
  </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:
 
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:

Revision as of 15:27, 26 April 2011


link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug Day
Contribute
Browse Source


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:

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


The following example shows how to define this mapping in EclipseLink's OXM metadata format:

...
<java-type name="Employee">
   <java-attributes>
      <xml-element java-attribute="name" type="java.lang.String"/>
      <xml-element java-attribute="name" type="java.lang.String"/>
 
   </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>
...


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:

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);


@XmlInverseReference back-pointers can be used with the following types of mappings:


@XmlInverseReference can be particularly useful when mapping JPA entities to XML. For an example, please see Binding JPA Relationships to XML.


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