Difference between revisions of "EclipseLink/UserGuide/MOXy/Mapping JPA Entities to XML/Bidirectional Relationships"

From Eclipsepedia

Jump to: navigation, search
m (New page: {{EclipseLink_UserGuide |info=y |toc=y |eclipselink=y |eclipselinktype=MOXy }} ==Relationships - Privately Owned== These relationships apply when the target object(s) is referenced by onl...)
 
m
Line 4: Line 4:
 
|eclipselink=y
 
|eclipselink=y
 
|eclipselinktype=MOXy
 
|eclipselinktype=MOXy
 +
|api=y
 +
|apis= * [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/XmlInverseReference.html XmlInverseReference]
 
}}
 
}}
  

Revision as of 10:17, 23 May 2011

EclipseLink MOXy

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

Contents


Relationships - Privately Owned

These relationships apply when the target object(s) is referenced by only a single source object. This type of relationship can be safely represented as nesting in XML (the default in JAXB).

One To One and Embedded

In JPA, the OneToOne and Embedded annotations indicate only one instance of the source entity is able to refer to the same target entity instance. Since the relationship between Employee and Address is bi-directional, use the EclipseLink extension @XmlInverseReference to represent the back-pointer.

@Entity
public class Employee {
 
    @OneToOne(mappedBy="resident")
    private Address residence;
 
}
 
@Entity
public class Address {
 
    @OneToOne
    @JoinColumn(name="E_ID")
    @XmlInverseReference(mappedBy="residence")
    private Employee resident;
 
}

One To Many

In JPA, the OneToMany annotation indicates that only one instance of the source entity can refer to the same target entity instance. Since the relationship between Employee and Address is bi-directional, use the EclipseLink extension @XmlInverseReference to map the back-pointer.

@Entity
public class Employee {
 
    @OneToMany(mappedBy="contact")
    private List<PhoneNumber> contactNumber;
 
}
 
@Entity
public class PhoneNumber {
 
    @ManyToOne
    @JoinColumn(name="E_ID", referencedColumnName = "E_ID")
    @XmlInverseReference(mappedBy="contactNumber")
    private Employee contact;
 
}


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