|
|
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=n
| + | |
− | |eclipselink=y
| + | |
− | |eclipselinktype=MOXy
| + | |
− | |api=y
| + | |
− | |apis= * [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/XmlJoinNodes.html XmlJoinNodes]
| + | |
− | * [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/XmlIDREF.html XmlIDREF]
| + | |
− | }}
| + | |
− | | + | |
− | | + | |
− | | + | |
− | =Keys and Foreign Keys=
| + | |
− | This example demonstrates how to derive an XML representation from a set of JPA entities using JAXB when a JPA entity has compound primary keys.
| + | |
− | | + | |
− | == JPA Entities ==
| + | |
− | | + | |
− | <source lang="java">
| + | |
− | @Entity
| + | |
− | public class PhoneNumber {
| + | |
− | | + | |
− | @ManyToOne
| + | |
− | @JoinColumns({
| + | |
− | @JoinColumn(name="E_ID", referencedColumnName = "E_ID"),
| + | |
− | @JoinColumn(name="E_COUNTRY", referencedColumnName = "COUNTRY")
| + | |
− | })
| + | |
− | | + | |
− | private Employee contact;
| + | |
− | | + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | <source lang="java">
| + | |
− | @Entity
| + | |
− | @IdClass(EmployeeId.class)
| + | |
− | public class Employee {
| + | |
− | | + | |
− | @Id
| + | |
− | @Column(name="E_ID")
| + | |
− | private BigDecimal eId;
| + | |
− | | + | |
− | @Id
| + | |
− | private String country;
| + | |
− | | + | |
− | @OneToMany(mappedBy="contact")
| + | |
− | private List<PhoneNumber> contactNumber;
| + | |
− | | + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | <source lang="java">
| + | |
− | public class EmployeeId {
| + | |
− | | + | |
− | private BigDecimal eId;
| + | |
− | private String country;
| + | |
− | | + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | == JAXB Bindings ==
| + | |
− | | + | |
− | 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.
| + | |
− | | + | |
− | <source lang="java">
| + | |
− | @XmlAccessorType(XmlAccessType.FIELD)
| + | |
− | package com.example.model;
| + | |
− | | + | |
− | import javax.xml.bind.annotation.XmlAccessType;
| + | |
− | import javax.xml.bind.annotation.XmlAccessorType;
| + | |
− | </source>
| + | |
− | | + | |
− | === Target Object ===
| + | |
− | Because this example requires support beyond the JAXB specification, you can use the EclipseLink extension '''@XmlKey'''. Because the relationship is bidirectional, use the EclipseLink extension '''@XmlInverseReference'''.
| + | |
− | | + | |
− | <source lang="java">
| + | |
− | @Entity
| + | |
− | @IdClass(EmployeeId.class)
| + | |
− | public class Employee {
| + | |
− | | + | |
− | @Id
| + | |
− | @Column(name="E_ID")
| + | |
− | @XmlID
| + | |
− | private BigDecimal eId;
| + | |
− | | + | |
− | @Id
| + | |
− | @XmlKey
| + | |
− | private String country;
| + | |
− | | + | |
− | @OneToMany(mappedBy="contact")
| + | |
− | @XmlInverseReference(mappedBy="contact")
| + | |
− | private List<PhoneNumber> contactNumber;
| + | |
− | | + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | === Source Object ===
| + | |
− | Because the target object has a compound key, use the EclipseLink extension '''@XmlJoinNodes''' to set up the mapping.
| + | |
− | | + | |
− | '''Note''': If the target object had a ''single'' ID, then you could use '''@XmlIDREF'''.
| + | |
− | | + | |
− | <source lang="java">
| + | |
− | @Entity
| + | |
− | public class PhoneNumber {
| + | |
− | | + | |
− | @ManyToOne
| + | |
− | @JoinColumns({
| + | |
− | @JoinColumn(name="E_ID", referencedColumnName = "E_ID"),
| + | |
− | @JoinColumn(name="E_COUNTRY", referencedColumnName = "COUNTRY")
| + | |
− | })
| + | |
− | @XmlJoinNodes( {
| + | |
− | @XmlJoinNode(xmlPath="contact/id/text()", referencedXmlPath="id/text()"),
| + | |
− | @XmlJoinNode(xmlPath="contact/country/text()", referencedXmlPath="country/text()")
| + | |
− | })
| + | |
− | private Employee contact;
| + | |
− | | + | |
− | }
| + | |
− | </source>
| + | |
− | | + | |
− | | + | |
− | {{EclipseLink_MOXy
| + | |
− | |previous= [[EclipseLink/UserGuide/MOXy/Mapping_JPA_Entities_to_XML/Bidirectional_Relationships|Bidirectional Relationships]]
| + | |
− | |up= [[EclipseLink/UserGuide/MOXy/Mapping_JPA_Entities_to_XML|Mapping JPA Entities to XML]]
| + | |
− | |next= [[EclipseLink/UserGuide/MOXy/Web_Services|Web Services]]
| + | |
− | |version=2.2.0 }}
| + | |