Skip to main content

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.

Jump to: navigation, search

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

< EclipseLink‎ | Examples‎ | MOXy‎ | JPA
(JAXB Bindings)
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
== Overview ==
 
== Overview ==
 +
 +
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 ==
 
== JPA Entities ==
Line 12: Line 14:
 
         @JoinColumn(name="E_COUNTRY", referencedColumnName = "COUNTRY")
 
         @JoinColumn(name="E_COUNTRY", referencedColumnName = "COUNTRY")
 
     })
 
     })
    @XmlIDREF
+
 
 
     private Employee contact;
 
     private Employee contact;
  
Line 25: Line 27:
 
     @Id
 
     @Id
 
     @Column(name="E_ID")
 
     @Column(name="E_ID")
    @XmlID
 
 
     private BigDecimal eId;
 
     private BigDecimal eId;
  
 
     @Id
 
     @Id
    @XmlID
 
 
     private String country;
 
     private String country;
  
 
     @OneToMany(mappedBy="contact")
 
     @OneToMany(mappedBy="contact")
    @XmlInverseReference(mappedBy="contact")
 
 
     private List<PhoneNumber> contactNumber;
 
     private List<PhoneNumber> contactNumber;
  
Line 58: Line 57:
 
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 ===
 +
 +
As we require support beyond the JAXB spec, we will make use of the EclipseLink extension @XmlKey.  Also since the relationship is bidirectional, we will 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 ===
 +
 +
If the target object had a single ID then we would use @XmlIDREF.  Since the target object has a compound key, we will use the EclipseLink extension @XmlJoinNodes to set up the mapping.
 +
 +
<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>
 
</source>

Latest revision as of 16:34, 16 March 2011

Overview

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

@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 {
 
    @Id
    @Column(name="E_ID")
    private BigDecimal eId;
 
    @Id
    private String country;
 
    @OneToMany(mappedBy="contact")
    private List<PhoneNumber> contactNumber;
 
}
public class EmployeeId {
 
    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

As we require support beyond the JAXB spec, we will make use of the EclipseLink extension @XmlKey. Also since the relationship is bidirectional, we will use the EclipseLink extension @XmlInverseReference.

@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 Object

If the target object had a single ID then we would use @XmlIDREF. Since the target object has a compound key, we will use the EclipseLink extension @XmlJoinNodes to set up the mapping.

@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;
 
}

Back to the top