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.
Difference between revisions of "EclipseLink/UserGuide/MOXy/Mapping JPA Entities to XML"
m (→JPA Entities) |
m (→Relationships - Privately Owned) |
||
Line 107: | Line 107: | ||
</source> | </source> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
==Relationships - Shared Reference== | ==Relationships - Shared Reference== |
Revision as of 14:30, 11 April 2011
EclipseLink MOXy
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Contents
Mapping JPA Entities to XML
This section demonstrates three strategies for mapping JPA entities to XML:
- Binding JPA Relationships to XML
- Binding Compound Primary Keys to XML
- Binding Embedded ID Classes to XML
Binding JPA Relationships to XML
This example demonstrates how to use JAXB to generate an XML representation from a set of JPA entities.
JAXB will 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:
Note: To save space, package names, import statements, and the get /set methods have been omitted. All annotations shown 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
In 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;
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. 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, use the EclipseLink extension @XmlInverseReference to represent the back-pointer.
@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") @XmlInverseReference(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. Since the relationship between Department and Employee is bi-directional, use the EclipseLink extension @XmlInverseReference to represent the back-pointer.
@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") @XmlInverseReference(mappedBy="member") private List<Department> team; }
Binding Compound Primay Keys to XML
Binding Embedded ID Classes to XML
|
|
|
EclipseLink Home MOXy User Guide: Table of Contents, Search | ||
How to contribute to this guide... |