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/Examples/MOXy/JPA/Relationships"

< EclipseLink‎ | Examples‎ | MOXy‎ | JPA
m
m (Replacing page with ''''Warning This page is obsolete. Please see ''[http://www.eclipse.org/eclipselink/documentation/ Developing JAXB Applications Using EclipseLink M...')
 
Line 1: Line 1:
{{EclipseLink_UserGuide
+
'''[[Image:Elug_draft_icon.png|Warning]] This page is obsolete. Please see ''[http://www.eclipse.org/eclipselink/documentation/ Developing JAXB Applications Using EclipseLink MOXy]'' for current information.'''
|info=y
+
|toc=y
+
|eclipselink=y
+
|eclipselinktype=MOXy
+
}}
+
== Overview ==
+
 
+
This example demonstrates how to derive an XML representation from a set of JPA entities using JAXB. In simplest terms JAXB will be used to apply a tree structure to the graph of JPA entities.  Multiple tree representations of a graph are possible and will depend on the root object chosen and the direction the relationships are traversed.
+
 
+
== JPA Entities ==
+
 
+
For this example the following JPA entities will be used.  In order to save space package names, import statements, and the get /set methods have been omitted from the following code samples.  All annotations shown below are standard JPA annotations.
+
 
+
<source lang="java">
+
@Entity
+
public class Department {
+
 
+
    @Id
+
    @Column(name="D_ID")
+
    private BigDecimal dId;
+
 
+
    private String name;
+
+
    @ManyToMany
+
    @JoinTable(name="DEPT_EMP", joinColumns =
+
        @JoinColumn(name="D_ID", referencedColumnName = "D_ID"),
+
            inverseJoinColumns = @JoinColumn(name="E_ID",
+
                referencedColumnName = "E_ID"))
+
    private List<Employee> member;
+
 
+
}
+
</source>
+
 
+
<source lang="java">
+
@Entity
+
public class Employee {
+
 
+
    @Id
+
    @Column(name="E_ID")
+
    private BigDecimal eId;
+
 
+
    private String name;
+
 
+
    @OneToOne(mappedBy="resident")
+
    private Address residence;
+
 
+
    @OneToMany(mappedBy="contact")
+
    private List<PhoneNumber> contactNumber;
+
 
+
    @ManyToMany(mappedBy="member")
+
    private List<Department> team;
+
 
+
}
+
</source>
+
 
+
<source lang="java">
+
@Entity
+
public class Address {
+
+
    @Id
+
    @Column(name="E_ID", insertable=false, updatable=false)
+
    private BigDecimal eId;
+
 
+
    private String city;
+
 
+
    private String street;
+
 
+
    @OneToOne
+
    @JoinColumn(name="E_ID")
+
    private Employee resident;
+
 
+
}
+
</source>
+
 
+
<source lang="java">
+
@Entity
+
@Table(name="PHONE_NUMBER")
+
public class PhoneNumber {
+
 
+
    @Id
+
    @Column(name="P_ID")
+
    private BigDecimal pId;
+
 
+
    @ManyToOne
+
    @JoinColumn(name="E_ID", referencedColumnName = "E_ID")
+
    private Employee contact;
+
 
+
    private String num;
+
 
+
}
+
</source>
+
 
+
== JAXB Binding ==
+
 
+
For this example the XML acessor type will be set to FIELD for all the model classes. This can be set as a package level JAXB annotation.
+
 
+
<source lang="java">
+
@XmlAccessorType(XmlAccessType.FIELD)
+
package com.example.model;
+
 
+
import javax.xml.bind.annotation.XmlAccessType;
+
import javax.xml.bind.annotation.XmlAccessorType;
+
</source>
+
 
+
=== Relationships - Privately Owned ===
+
 
+
These relationships apply when the target object(s) is only referenced by 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 that 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, the EclipseLink extension @XmlInverseReference can be used to represent the back-pointer.
+
 
+
<source lang="java">
+
@Entity
+
public class Employee {
+
 
+
    @OneToOne(mappedBy="resident")
+
    private Address residence;
+
 
+
}
+
</source>
+
 
+
<source lang="java">
+
@Entity
+
public class Address {
+
+
    @OneToOne
+
    @JoinColumn(name="E_ID")
+
    @XmlInverseReference(mappedBy="residence")
+
    private Employee resident;
+
 
+
}
+
</source>
+
 
+
==== One To Many ====
+
 
+
In JPA the OneToMany annotation indicates that 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, we can again use the EclipseLink extension @XmlInverseReference to map the back-pointer.
+
 
+
<source lang="java">
+
@Entity
+
public class Employee {
+
 
+
    @OneToMany(mappedBy="contact")
+
    private List<PhoneNumber> contactNumber;
+
 
+
}
+
</source>
+
 
+
<source lang="java">
+
@Entity
+
public class PhoneNumber {
+
 
+
    @ManyToOne
+
    @JoinColumn(name="E_ID", referencedColumnName = "E_ID")
+
    @XmlInverseReference(mappedBy="contactNumber")
+
    private Employee contact;
+
 
+
}
+
</source>
+
 
+
=== Relationships - Shared Reference ===
+
 
+
These relationships apply when the target object(s) may be referenced by multiple source objects.  This type of relationship can not be safely represented as nesting in XML.  So instead key relationships are used.  In order to leverage the ID fields on JPA entities, EclipseLink JAXB allows the @XmlID annotation to be used on non-String fields/properties.
+
 
+
==== Many To One ====
+
 
+
In JPA the ManyToOne annotation indicates that one or more instances of the source entity are able to refer to the same target entity instance.  Since the relationship between PhoneNumber and Employee is bi-directional, the EclipseLink extension @XmlInverseReference can be used to represent the back-pointer
+
 
+
<source lang="java">
+
@Entity
+
public class PhoneNumber {
+
 
+
    @ManyToOne
+
    @JoinColumn(name="E_ID", referencedColumnName = "E_ID")
+
    @XmlIDREF
+
    private Employee contact;
+
 
+
}
+
</source>
+
 
+
<source lang="java">
+
@Entity
+
public class Employee {
+
 
+
    @Id
+
    @Column(name="E_ID")
+
    @XmlID
+
    private BigDecimal eId;
+
 
+
    @OneToMany(mappedBy="contact")
+
    @XmlInverseReference(mappedBy="contact")
+
    private List<PhoneNumber> contactNumber;
+
 
+
}
+
</source>
+
 
+
==== Many To Many ====
+
 
+
In JPA the ManyToMany annotation indicates that one or more instances of the source entity are able to refer to the same target entity instance. Since the relationship between Department and Employee is bi-directional, the EclipseLink extension @XmlInverseReference can be used to represent the back-pointer
+
 
+
<source lang="java">
+
@Entity
+
public class Department {
+
+
    @ManyToMany
+
    @JoinTable(name="DEPT_EMP", joinColumns =
+
        @JoinColumn(name="D_ID", referencedColumnName = "D_ID"),
+
            inverseJoinColumns = @JoinColumn(name="E_ID",
+
                referencedColumnName = "E_ID"))
+
    @XmlIDREF
+
    private List<Employee> member;
+
 
+
}
+
</source>
+
 
+
<source lang="java">
+
@Entity
+
public class Employee {
+
 
+
    @Id
+
    @Column(name="E_ID")
+
    @XmlId
+
    private BigDecimal eId;
+
 
+
    @ManyToMany(mappedBy="member")
+
    @XmlInverseReference(mappedBy="member")
+
    private List<Department> team;
+
 
+
}
+
</source>
+

Latest revision as of 13:20, 30 January 2013

Warning This page is obsolete. Please see Developing JAXB Applications Using EclipseLink MOXy for current information.

Copyright © Eclipse Foundation, Inc. All Rights Reserved.