|
|
(34 intermediate revisions by the same user not shown) |
Line 1: |
Line 1: |
− | == Overview == | + | == Converting JPA entities to/from XML (via JAXB) == |
− | | + | JPA provides an easy and powerful means to use Java objects to interact with a relational database. These Java objects are called entities, and have their own characteristics. Some of these characteristics (bidirectional relationships, compound keys, embedded key classes, and lazy loading) can cause challenges when mapping these objects to XML. This example demonstrates how to easily overcome these challenges using EclipseLink MOXy (JAXB): |
− | This example demonstrates how to derive an XML representation from a set of JPA entities using JAXB.
| + | * [[EclipseLink/Examples/MOXy/JPA/Relationships | Binding JPA Relationships to XML]] |
− | | + | * [[EclipseLink/Examples/MOXy/JPA/CompoundPrimaryKeys | Binding Compound Primay Keys to XML]] |
− | == JPA Entities ==
| + | * [[EclipseLink/Examples/MOXy/JPA/EmbeddedIdClass | Binding Embedded ID Classes to XML]] |
− | | + | |
− | 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 ==
| + | |
− | | + | |
− | === 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.
| + | |
− | | + | |
− | <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")
| + | |
− | private Employee resident;
| + | |
− | | + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | === 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.
| + | |
− | | + | |
− | <source lang="java">
| + | |
− | @Entity
| + | |
− | public class PhoneNumber {
| + | |
− | | + | |
− | @ManyToOne
| + | |
− | @JoinColumn(name="E_ID", referencedColumnName = "E_ID")
| + | |
− | private Employee contact;
| + | |
− | | + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | <source lang="java">
| + | |
− | @Entity
| + | |
− | public class Employee {
| + | |
− | | + | |
− | @OneToMany(mappedBy="contact")
| + | |
− | private List<PhoneNumber> contactNumber;
| + | |
− | | + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | === 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.
| + | |
− | | + | |
− | <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")
| + | |
− | private Employee contact;
| + | |
− | | + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | === 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.
| + | |
− | | + | |
− | <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 {
| + | |
− | | + | |
− | @ManyToMany(mappedBy="member")
| + | |
− | private List<Department> team;
| + | |
− | | + | |
− | }
| + | |
− | </source>
| + | |
JPA provides an easy and powerful means to use Java objects to interact with a relational database. These Java objects are called entities, and have their own characteristics. Some of these characteristics (bidirectional relationships, compound keys, embedded key classes, and lazy loading) can cause challenges when mapping these objects to XML. This example demonstrates how to easily overcome these challenges using EclipseLink MOXy (JAXB):