Skip to main content

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.

Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/MOXy/Relationships/Shared Reference/Keys and Foreign Keys/Composite Key"

Line 3: Line 3:
 
|api=y
 
|api=y
 
|toc=y
 
|toc=y
|apis= * [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/XmlAccessType.html javax.xml.bind.annotation.XmlAccessType]
+
|apis= * [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/oxm/annotations/XmlKey.html @XmlKey]
* [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/XmlCustomizer.html javax.xml.bind.annotation.XmlCustomizer]
+
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/oxm/annotations/XmlJoinNodes.html @XmlJoinNodes]
* [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/XmlInverseReference.html javax.xml.bind.annotation.XmlInverseReference]
+
 
|eclipselink=y
 
|eclipselink=y
 
|eclipselinktype=MOXy
 
|eclipselinktype=MOXy
Line 48: Line 47:
 
...
 
...
 
<java-type name="Employee">
 
<java-type name="Employee">
  <java-attributes>
+
    <java-attributes>
      <xml-attribute java-attribute="id" type="java.lang.Integer" xml-id="true"/>
+
        <xml-attribute java-attribute="id" xml-id="true" />
      <xml-attribute java-attribute="name" type="java.lang.String"/>
+
        <xml-attribute java-attribute="name" xml-key="true" />
      <xml-element java-attribute="manager" type="mypackage.Employee" xml-idref="true"/>
+
        <xml-join-nodes java-attribute="manager">
      <xml-element java-attribute="reports" type="mypackage.Employee" container-type="java.util.ArrayList" xml-idref="true"/>
+
            <xml-join-node xml-path="manager/@id" referenced-xml-path="@id" />
  </java-attributes>
+
            <xml-join-node xml-path="manager/@name" referenced-xml-path="@name" />
 +
        </xml-join-nodes>
 +
        <xml-join-nodes java-attribute="reports" container-type="java.util.ArrayList">
 +
            <xml-join-node xml-path="report/@id" referenced-xml-path="@id" />
 +
            <xml-join-node xml-path="report/@name" referenced-xml-path="@name" />
 +
        </xml-join-nodes>
 +
    </java-attributes>
 
</java-type>
 
</java-type>
 
...
 
...
Line 60: Line 65:
 
This would produce the following XML:
 
This would produce the following XML:
  
<source lang="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>
 
</source>
 
 
The '''manager''' and '''reports''' elements contain the IDs of the '''Employee''' instances they are referencing.
 
 
 
== Using @XmlList ==
 
 
Because the '''@XmlIDREF''' annotation is also compatible with the '''@XmlList''' annotation, the '''Employee''' object could be modeled as:
 
 
<source lang="java">
 
@XmlAccessorType(XmlAccessType.FIELD)
 
public class Employee {
 
    @XmlID
 
    @XmlAttribute
 
    private Integer id;
 
 
    @XmlAttribute
 
    private String name;
 
 
    @XmlIDREF
 
    private Employee manager;
 
 
    @XmlIDREF
 
    @XmlList
 
    private List<Employee> reports;
 
 
    ...
 
}
 
</source>
 
 
This would produce the following XML:
 
 
<source lang="xml">
 
<source lang="xml">
 
<company>
 
<company>
 
   <employee id="1" name="Jane Doe">
 
   <employee id="1" name="Jane Doe">
       <reports>2 3</reports>
+
       <report id="2" name="John Smith"/>
 +
      <report id="3" name="Anne Jones"/>
 
   </employee>
 
   </employee>
 
   <employee id="2" name="John Smith">
 
   <employee id="2" name="John Smith">
       <manager>1</manager>
+
       <manager id="1" name="Jane Doe"/>
 
   </employee>
 
   </employee>
 
   <employee id="3" name="Anne Jones">
 
   <employee id="3" name="Anne Jones">
       <manager>1</manager>
+
       <manager id="1" name="Jane Doe"/>
 
   </employee>
 
   </employee>
</company>
+
</company>  
 
</source>
 
</source>
 +
  
 
{{EclipseLink_MOXy
 
{{EclipseLink_MOXy

Revision as of 16:08, 16 March 2011

EclipseLink MOXy

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source

Mapping Composite Key Relationships

If the objects that you want to map have multi-part keys (i.e., a combination of fields that determines uniqueness), you can use EclipseLink's @XmlKey and @XmlJoinNodes to set up this relationship.

One or more @XmlKey annotations can be used to declare the primary keys in a given class. For a single key, either @XmlID or @XmlKey can be used. For composite primary keys, multiple @XmlKey annotations can be used, or a single @XmlID can be combined with one or more @XmlKey annotations.

In this example, each Employee has one manager but multiple reports, and Employees are uniquely identified by the combination of their id and name fields.

@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
    @XmlID
    @XmlAttribute
    private Integer id;
 
    @XmlKey
    @XmlAttribute
    private String name;
 
    @XmlJoinNodes( {
        @XmlJoinNode(xmlPath = "manager/@id", referencedXmlPath = "@id"),
        @XmlJoinNode(xmlPath = "manager/@name", referencedXmlPath = "@name") })
    public Employee manager;
 
    @XmlJoinNodes( {
        @XmlJoinNode(xmlPath = "report/@id", referencedXmlPath = "@id"),
        @XmlJoinNode(xmlPath = "report/@name", referencedXmlPath = "@name") })
    public List<Employee> reports = new ArrayList<Employee>();
 
    ...
}

The following example shows how to define this mapping information in EclipseLink's OXM metadata format.

...
<java-type name="Employee">
    <java-attributes>
        <xml-attribute java-attribute="id" xml-id="true" />
        <xml-attribute java-attribute="name" xml-key="true" />
        <xml-join-nodes java-attribute="manager">
            <xml-join-node xml-path="manager/@id" referenced-xml-path="@id" />
            <xml-join-node xml-path="manager/@name" referenced-xml-path="@name" />
        </xml-join-nodes>
        <xml-join-nodes java-attribute="reports" container-type="java.util.ArrayList">
            <xml-join-node xml-path="report/@id" referenced-xml-path="@id" />
            <xml-join-node xml-path="report/@name" referenced-xml-path="@name" />
        </xml-join-nodes>
    </java-attributes>
</java-type>
...

This would produce the following XML:

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


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

Back to the top