Jump to: navigation, search

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

Line 52: Line 52:
 
* [[EclipseLink/UserGuide/MOXy/Relationships/Shared_Reference/Keys_and_Foreign_Keys/Composite_Key|Composite 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 [].
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
When binding JPA entities to XML, there are MOXy extensions will help you map a bidirectional relationship.
+
 
+
In this sample model, notice that '''Customer''' has a relationship to '''Address''', and '''Address''' has a relationship ''back'' to '''Customer'''. In JPA, one direction of the relationship is mapped (the '''customer''' property on '''Address'''); the other direction specifies a mapping to leverage.
+
 
+
'''Sample Model'''
+
<source lang="Java">
+
import javax.persistence.*;
+
+
@Entity
+
public class Customer {
+
+
    @Id
+
    private long id;
+
+
    @OneToOne(mappedBy="customer", cascade={CascadeType.ALL})
+
    private Address address;
+
+
}
+
 
+
+
import javax.persistence.*;
+
+
@Entity
+
public class Address implements Serializable {
+
+
    @Id
+
    private long id;
+
+
    @MapsId
+
    @OneToOne
+
    @JoinColumn(name="ID")
+
    private Customer customer;
+
+
}
+
</source>
+
 
+
To marshal these objects to XML, mark one direction '''@XmlTransient''' to prevent a JAXB infinite loop during marshalling. Normally, during unmarshalling (from XML-to-object), '''you'' are responsible for populating the back pointer
+
 
+
<source lang="Java">
+
+
import javax.persistence.*;
+
import javax.xml.bind.annotation.*;
+
   
+
@Entity
+
public class Address implements Serializable {
+
+
    @Id
+
    private long id;
+
+
    @OneToOne
+
    @JoinColumn(name="ID")
+
    @MapsId
+
    @XmlTransient
+
    private Customer customer;
+
+
}
+
</source>
+
 
+
 
+
 
+
==@XMLInverseReference==
+
With the '''@XmlInverseReference''' annotation, MOXy will populate the back pointer automatically. In this sample, notice that the '''@XmlInverseReference''' annotation leverages the same "mappedBy" concept.
+
 
+
<source lang="java">
+
+
import javax.persistence.*;
+
import org.eclipse.persistence.oxm.annotations.*;
+
+
@Entity
+
public class Address implements Serializable {
+
+
    @Id
+
    private long id;
+
+
    @OneToOne
+
    @JoinColumn(name="ID")
+
    @MapsId
+
    @XmlInverseReference(mappedBy="address")
+
    private Customer customer;
+
+
}
+
</source>
+
 
+
  
  

Revision as of 14:22, 19 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;
   ...
}

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 [].


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