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/UserGuide/MOXy/Relationships/Shared Reference/Keys and Foreign Keys/Single Key"

Line 10: Line 10:
 
To model non-privately-owned relationships, your "target" objects must have IDs (keys) defined, and your "source" object must use these IDs to map the relationship.
 
To model non-privately-owned relationships, your "target" objects must have IDs (keys) defined, and your "source" object must use these IDs to map the relationship.
  
Relationships represented with keys use the '''XmlID''' and '''XmlIDREF''' annotations.  Although the JAXB specification requires that the property marked with '''@XmlID''' be a '''String''', MOXy JAXB does not enforce this restriction.  
+
Relationships represented with keys use the '''@XmlID''' and '''@XmlIDREF''' annotations.  Although the JAXB specification requires that the property marked with '''@XmlID''' be a '''String''', MOXy JAXB does not enforce this restriction.  
  
 
In this example, each '''Employee''' has ''one'' '''manager''' but ''multiple'' '''reports'''.
 
In this example, each '''Employee''' has ''one'' '''manager''' but ''multiple'' '''reports'''.
 
<source lang="java">
 
<source lang="java">
import java.util.ArrayList;
 
import java.util.List;
 
 
import javax.xml.bind.annotation.XmlAccessorType;
 
import javax.xml.bind.annotation.XmlAccessType;
 
import javax.xml.bind.annotation.XmlAttribute;
 
import javax.xml.bind.annotation.XmlElement;
 
import javax.xml.bind.annotation.XmlID;
 
import javax.xml.bind.annotation.XmlIDREF;
 
 
 
@XmlAccessorType(XmlAccessType.FIELD)
 
@XmlAccessorType(XmlAccessType.FIELD)
 
public class Employee {
 
public class Employee {
 
 
     @XmlAttribute
 
     @XmlAttribute
 
     @XmlID
 
     @XmlID
Line 40: Line 29:
 
     @XmlIDREF
 
     @XmlIDREF
 
     private List<Employee> reports;
 
     private List<Employee> reports;
+
 
     public Employee() {
+
     ...
        reports = new ArrayList<Employee>();
+
    }
+
+
 
}
 
}
 
</source>
 
</source>
  
 
Would produce the following XML:
 
Would produce the following XML:
 +
 
<source lang="xml">
 
<source lang="xml">
 
<company>
 
<company>
Line 63: Line 50:
 
</company>  
 
</company>  
 
</source>
 
</source>
The manager and reports are marshalled with the ID of the employee instance they are referencing.
 
  
Because the '''@XmlIDREF''' annotation is also compatible with the '''@XmlList''' annotation, the Employee object could be modeled as:
+
The '''manager''' and '''reports''' elements contain the IDs of the '''Employee''' instances they are referencing.
 +
 
 +
Because the '''@XmlIDREF''' annotation is also compatible with the '''@XmlList''' annotation, the '''Employee''' object could be modeled as:
 +
 
 
<source lang="java">
 
<source lang="java">
package blog.shared;
 
 
import java.util.ArrayList;
 
import java.util.List;
 
 
import javax.xml.bind.annotation.XmlAccessorType;
 
import javax.xml.bind.annotation.XmlAccessType;
 
import javax.xml.bind.annotation.XmlAttribute;
 
import javax.xml.bind.annotation.XmlID;
 
import javax.xml.bind.annotation.XmlIDREF;
 
import javax.xml.bind.annotation.XmlList;
 
 
@XmlAccessorType(XmlAccessType.FIELD)
 
 
public class Employee {
 
public class Employee {
 
 
     @XmlID
 
     @XmlID
 
     @XmlAttribute
 
     @XmlAttribute
Line 95: Line 70:
 
     @XmlList
 
     @XmlList
 
     private List<Employee> reports;
 
     private List<Employee> reports;
+
 
     public Employee() {
+
     ...
        reports = new ArrayList<Employee>();
+
    }
+
+
 
}
 
}
 
</source>  
 
</source>  

Revision as of 11:11, 16 March 2011



Single Key

To model non-privately-owned relationships, your "target" objects must have IDs (keys) defined, and your "source" object must use these IDs to map the relationship.

Relationships represented with keys use the @XmlID and @XmlIDREF annotations. Although the JAXB specification requires that the property marked with @XmlID be a String, MOXy JAXB does not enforce this restriction.

In this example, each Employee has one manager but multiple reports.

@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
    @XmlAttribute
    @XmlID
    private String id;
 
    @XmlAttribute
    private String name;
 
    @XmlIDREF
    private Employee manager;
 
    @XmlElement(name="report")
    @XmlIDREF
    private List<Employee> reports;
 
    ...
}

Would produce the following XML:

<company>
    <employee id="1" name="Jane Doe">
        <report>2</report>
        <report>3</report>
    </employee>
    <employee id="2" name="John Smith">
        <manager>1</manager>
    </employee>
    <employee id="3" name="Anne Jones">
        <manager>1</manager>
    </employee>
</company>

The manager and reports elements contain the IDs of the Employee instances they are referencing.

Because the @XmlIDREF annotation is also compatible with the @XmlList annotation, the Employee object could be modeled as:

public class Employee {
    @XmlID
    @XmlAttribute
    private String id;
 
    @XmlAttribute
    private String name;
 
    @XmlIDREF
    private Employee manager;
 
    @XmlIDREF
    @XmlList
    private List<Employee> reports;
 
    ... 
}

Which would produce the following XML:

<company>
   <employee id="1" name="Jane Doe">
      <reports>2 3</reports>
   </employee>
   <employee id="2" name="John Smith">
      <manager>1</manager>
   </employee>
   <employee id="3" name="Anne Jones">
      <manager>1</manager>
   </employee>
</company>

Eclipselink-logo.gif
Version: 2.2.0
Other versions...

Back to the top