Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/MOXy/JPA"

m (JPA Entities)
(JAXB Binding)
Line 88: Line 88:
 
== JAXB Binding ==
 
== JAXB Binding ==
  
For this example the XML acessor type will be set to FIELD for all the model classes.
+
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 annotation.
 +
 
 +
<source lang="java">
 +
@XmlAccessorType(XmlAccessType.FIELD)
 +
package com.example.model;
 +
 
 +
import javax.xml.bind.annotation.XmlAccessType;
 +
import javax.xml.bind.annotation.XmlAccessorType;
 +
</source>
  
 
=== Relationship – One To One (Containment) ===
 
=== Relationship – One To One (Containment) ===
Line 96: Line 104:
 
<source lang="java">
 
<source lang="java">
 
@Entity
 
@Entity
@XmlAccessorType(XmlAccessType.FIELD)
 
 
public class Employee {
 
public class Employee {
  
Line 107: Line 114:
 
<source lang="java">
 
<source lang="java">
 
@Entity
 
@Entity
@XmlAccessorType(XmlAccessType.FIELD)
 
 
public class Address {
 
public class Address {
 
 
Line 124: Line 130:
 
<source lang="java">
 
<source lang="java">
 
@Entity
 
@Entity
@XmlAccessorType(XmlAccessType.FIELD)
 
 
public class PhoneNumber {
 
public class PhoneNumber {
  
Line 136: Line 141:
 
<source lang="java">
 
<source lang="java">
 
@Entity
 
@Entity
@XmlAccessorType(XmlAccessType.FIELD)
 
 
public class Employee {
 
public class Employee {
  
Line 151: Line 155:
 
<source lang="java">
 
<source lang="java">
 
@Entity
 
@Entity
@XmlAccessorType(XmlAccessType.FIELD)
 
 
public class Employee {
 
public class Employee {
  
Line 162: Line 165:
 
<source lang="java">
 
<source lang="java">
 
@Entity
 
@Entity
@XmlAccessorType(XmlAccessType.FIELD)
 
 
public class PhoneNumber {
 
public class PhoneNumber {
  
Line 179: Line 181:
 
<source lang="java">
 
<source lang="java">
 
@Entity
 
@Entity
@XmlAccessorType(XmlAccessType.FIELD)
 
 
public class Department {
 
public class Department {
  
Line 200: Line 201:
 
<source lang="java">
 
<source lang="java">
 
@Entity
 
@Entity
@XmlAccessorType(XmlAccessType.FIELD)
 
 
public class Employee {
 
public class Employee {
  

Revision as of 09:43, 1 October 2009

Overview

This example demonstrates how to derive an XML representation from a set of JPA entities using JAXB.

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 annotation.

@XmlAccessorType(XmlAccessType.FIELD)
package com.example.model;
 
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

Relationship – One To One (Containment)

This relationship applies when an object privately owns another data object. In JPA the OneToOne annotation indicates that only one instance of the source entity is able to refer to the same target entity instance. This type of relationship can safely be represented through nesting in XML (the default in JAXB). 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 {
 
    @OneToOne(mappedBy="resident")
    private Address residence;
 
}
@Entity
public class Address {
 
    @OneToOne
    @JoinColumn(name="E_ID")
    @XmlTransient
    private Employee resident;
 
}

Relationship – One To One (Non-Containment)

This relationship applies when an object references but does not privately own another data object. 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")
    private Employee contact;
 
}
@Entity
public class Employee {
 
    @OneToMany(mappedBy="contact")
    private List<PhoneNumber> contactNumber;
 
}

Relationship – One To Many (Containment)

This relationship applies when an object privately owns a collection of data objects. In JPA the OneToMany annotation indicates that only one instance of the source entity is able to refer to the same target entity instance. This type of relationship can safely be represented through nesting in XML (the default in JAXB). 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;
 
}

Relationship – One To Many (Non-Containment)

This relationship applies when an object references but does not privately own a collection of data objects. 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 {
 
    @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 {
 
    @ManyToMany(mappedBy="member")
    private List<Department> team;
 
}