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/Examples/MOXy/JPA"
(→Many To One) |
(→Many To Many) |
||
Line 197: | Line 197: | ||
@Entity | @Entity | ||
public class Department { | public class Department { | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
@ManyToMany | @ManyToMany | ||
Line 209: | Line 203: | ||
inverseJoinColumns = @JoinColumn(name="E_ID", | inverseJoinColumns = @JoinColumn(name="E_ID", | ||
referencedColumnName = "E_ID")) | referencedColumnName = "E_ID")) | ||
+ | @XmlIDREF | ||
private List<Employee> member; | private List<Employee> member; | ||
Line 217: | Line 212: | ||
@Entity | @Entity | ||
public class Employee { | public class Employee { | ||
+ | |||
+ | @Id | ||
+ | @Column(name="E_ID") | ||
+ | @XmlId | ||
+ | private BigDecimal eId; | ||
@ManyToMany(mappedBy="member") | @ManyToMany(mappedBy="member") |
Revision as of 11:33, 1 October 2009
Contents
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 object graph of JPA entities. Multiple tree representations of an object 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.
@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; }
@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; }
@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; }
@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; }
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.
@XmlAccessorType(XmlAccessType.FIELD) package com.example.model; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType;
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
In JPA the OneToOne 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 need to mark one side transient in JAXB to prevent a cycle. In order to populate the relationship between Address and Employee the EclipseLink annotation @XmlContainerProperty can be used.
@Entity public class Employee { @OneToOne(mappedBy="resident") @XmlContainerProperty("resident") private Address residence; }
@Entity public class Address { @OneToOne @JoinColumn(name="E_ID") @XmlTransient private Employee resident; }
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 need to mark one side transient in JAXB to prevent a cycle.
@Entity public class Employee { @OneToMany(mappedBy="contact") private List<PhoneNumber> contactNumber; }
@Entity public class PhoneNumber { @ManyToOne @JoinColumn(name="E_ID", referencedColumnName = "E_ID") @XmlTransient private Employee contact; }
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.
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.
@Entity public class PhoneNumber { @ManyToOne @JoinColumn(name="E_ID", referencedColumnName = "E_ID") @XmlIDREF private Employee contact; }
@Entity public class Employee { @Id @Column(name="E_ID") @XmlID private BigDecimal eId; @OneToMany(mappedBy="contact") private List<PhoneNumber> contactNumber; }
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.
@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; }
@Entity public class Employee { @Id @Column(name="E_ID") @XmlId private BigDecimal eId; @ManyToMany(mappedBy="member") private List<Department> team; }