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')
 
(16 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, one of the properties must be annotated as an '''@XmlInverseReference'''.  Without this annotation, the cyclic relationship will result in an infinite loop during marshalling.
+
 
+
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>
+
 
+
 
+
 
+
{{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

Copyright © Eclipse Foundation, Inc. All Rights Reserved.