Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/UserGuide/MOXy/Relationships/Bidirectional Relationships"
m |
m |
||
Line 4: | Line 4: | ||
|apis= | |apis= | ||
}} | }} | ||
− | = Bidirectional Relationships = | + | = Bidirectional Relationships = |
+ | When binding JPA entities to XML | ||
+ | |||
+ | JAXB is your best choice when binding Java objects to XML. But what happens when those Java objects turn out to be JPA entities? For the most part it is business as usual, although there are a couple things to be aware of. In this post we'll cover a MOXy extension that can be used to 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''' | ||
+ | <soure 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> | ||
+ | |||
+ | 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 11:30, 5 April 2011
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Bidirectional Relationships
When binding JPA entities to XML
JAXB is your best choice when binding Java objects to XML. But what happens when those Java objects turn out to be JPA entities? For the most part it is business as usual, although there are a couple things to be aware of. In this post we'll cover a MOXy extension that can be used to 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 <soure 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
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; }
With the @XmlInverseReference annotation, MOXy will populate the back pointer automatically. In this sample, notice that the @XmlInverseReference annotation leverages the same "mappedBy" concept.
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; }