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/EmbeddedIdClass"
< EclipseLink | Examples | MOXy | JPA
(→JPA Entities) |
(→JAXB Bindings) |
||
Line 55: | Line 55: | ||
import javax.xml.bind.annotation.XmlAccessType; | import javax.xml.bind.annotation.XmlAccessType; | ||
import javax.xml.bind.annotation.XmlAccessorType; | import javax.xml.bind.annotation.XmlAccessorType; | ||
+ | </source> | ||
+ | |||
+ | === Target Object === | ||
+ | |||
+ | <source lang="java"> | ||
+ | @Entity | ||
+ | @IdClass(EmployeeId.class) | ||
+ | @XmlCustomizer(EmployeeCustomizer.class) | ||
+ | public class Employee { | ||
+ | |||
+ | @EmbeddedId | ||
+ | private EmployeeId id; | ||
+ | |||
+ | @OneToMany(mappedBy="contact") | ||
+ | @XmlInverseReference(mappedBy="contact") | ||
+ | private List<PhoneNumber> contactNumber; | ||
+ | |||
+ | } | ||
+ | </source> | ||
+ | |||
+ | <source lang="java"> | ||
+ | import org.eclipse.persistence.config.DescriptorCustomizer; | ||
+ | import org.eclipse.persistence.descriptors.ClassDescriptor; | ||
+ | import org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping; | ||
+ | |||
+ | public class EmployeeCustomizer implements DescriptorCustomizer { | ||
+ | |||
+ | public void customize(ClassDescriptor descriptor) throws Exception { | ||
+ | XMLCompositeObjectMapping idMapping = (XMLCompositeObjectMapping) descriptor.getMappingForAttributeName("id"); | ||
+ | idMapping.setXPath("."); | ||
+ | |||
+ | descriptor.addPrimaryKeyFieldName("eId/text()"); | ||
+ | descriptor.addPrimaryKeyFieldName("country/text()"); | ||
+ | } | ||
+ | |||
+ | }</source> | ||
+ | |||
+ | === Source Object === | ||
+ | |||
+ | <source lang="java"> | ||
+ | @Entity | ||
+ | @XmlCustomizer(PhoneNumberCustomizer.class) | ||
+ | public class PhoneNumber { | ||
+ | |||
+ | @ManyToOne | ||
+ | @JoinColumns({ | ||
+ | @JoinColumn(name="E_ID", referencedColumnName = "E_ID"), | ||
+ | @JoinColumn(name="E_COUNTRY", referencedColumnName = "COUNTRY") | ||
+ | }) | ||
+ | @XmlTransient | ||
+ | private Employee contact; | ||
+ | |||
+ | } | ||
+ | </source> | ||
+ | |||
+ | <source lang="java"> | ||
+ | import org.eclipse.persistence.config.DescriptorCustomizer; | ||
+ | import org.eclipse.persistence.descriptors.ClassDescriptor; | ||
+ | import org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping; | ||
+ | |||
+ | public class PhoneNumberCustomizer implements DescriptorCustomizer { | ||
+ | |||
+ | public void customize(ClassDescriptor descriptor) throws Exception { | ||
+ | XMLObjectReferenceMapping contactMapping = new XMLObjectReferenceMapping(); | ||
+ | contactMapping.setAttributeName("contact"); | ||
+ | contactMapping.setReferenceClass(Employee.class); | ||
+ | contactMapping.addSourceToTargetKeyFieldAssociation("contact/@eID", "eId/text()"); | ||
+ | contactMapping.addSourceToTargetKeyFieldAssociation("contact/@country", "country/text()"); | ||
+ | descriptor.addMapping(contactMapping); | ||
+ | } | ||
+ | |||
+ | } | ||
</source> | </source> |
Revision as of 16:56, 1 December 2009
Overview
This example demonstrates how to derive an XML representation from a set of JPA entities using JAXB when a JPA entity has an embedded ID class.
JPA Entities
@Entity public class PhoneNumber { @ManyToOne @JoinColumns({ @JoinColumn(name="E_ID", referencedColumnName = "E_ID"), @JoinColumn(name="E_COUNTRY", referencedColumnName = "COUNTRY") }) private Employee contact; }
@Entity @IdClass(EmployeeId.class) public class Employee { @EmbeddedId private EmployeeId id; @OneToMany(mappedBy="contact") private List<PhoneNumber> contactNumber; }
@Embeddable public class EmployeeId { @Column(name="E_ID") private BigDecimal eId; private String country; }
JAXB Bindings
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;
Target Object
@Entity @IdClass(EmployeeId.class) @XmlCustomizer(EmployeeCustomizer.class) public class Employee { @EmbeddedId private EmployeeId id; @OneToMany(mappedBy="contact") @XmlInverseReference(mappedBy="contact") private List<PhoneNumber> contactNumber; }
import org.eclipse.persistence.config.DescriptorCustomizer; import org.eclipse.persistence.descriptors.ClassDescriptor; import org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping; public class EmployeeCustomizer implements DescriptorCustomizer { public void customize(ClassDescriptor descriptor) throws Exception { XMLCompositeObjectMapping idMapping = (XMLCompositeObjectMapping) descriptor.getMappingForAttributeName("id"); idMapping.setXPath("."); descriptor.addPrimaryKeyFieldName("eId/text()"); descriptor.addPrimaryKeyFieldName("country/text()"); } }
Source Object
@Entity @XmlCustomizer(PhoneNumberCustomizer.class) public class PhoneNumber { @ManyToOne @JoinColumns({ @JoinColumn(name="E_ID", referencedColumnName = "E_ID"), @JoinColumn(name="E_COUNTRY", referencedColumnName = "COUNTRY") }) @XmlTransient private Employee contact; }
import org.eclipse.persistence.config.DescriptorCustomizer; import org.eclipse.persistence.descriptors.ClassDescriptor; import org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping; public class PhoneNumberCustomizer implements DescriptorCustomizer { public void customize(ClassDescriptor descriptor) throws Exception { XMLObjectReferenceMapping contactMapping = new XMLObjectReferenceMapping(); contactMapping.setAttributeName("contact"); contactMapping.setReferenceClass(Employee.class); contactMapping.addSourceToTargetKeyFieldAssociation("contact/@eID", "eId/text()"); contactMapping.addSourceToTargetKeyFieldAssociation("contact/@country", "country/text()"); descriptor.addMapping(contactMapping); } }