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

EclipseLink/DesignDocs/293925/MOXyExtensions/XmlCollectionReferenceMapping

< EclipseLink‎ | DesignDocs‎ | 293925‎ | MOXyExtensions
Revision as of 15:48, 2 June 2010 by David.mccann.oracle.com (Talk | contribs) (Design)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

XMLCollectionReferenceMapping

Requirements

Provide support for XML collection reference mapping configuration via XML metadata file.

The following should be configurable:

Design

Basic XML collection reference mapping support

We will extend our current xml-element and xml-attribute support to allow collection reference mapping configuration. For example, the following XML metadata snippet would be used to setup a collection reference mapping for 'workAddresses':

<xml-element java-attribute="workAddresses" xml-idref="true" />

Where the following exists in Address:

<xml-attribute java-attribute="id" xml-id="true" required="true" />

If workAddresses was to be mapped to an element named work-address-id, then the following would be used:

<xml-element java-attribute="workAddresses" xml-path="work-address-id/text()" xml-idref="true" />

Where the following exists in Address:

<xml-attribute java-attribute="id" xml-id="true" required="true" />

Read Only

The following demonstrates how a collection reference mapping can be set as read-only:

<xml-element java-attribute="workAddresses" xml-idref="true" read-only="true">

Write Only

The following demonstrates how a collection reference mapping can be set as write-only:

<xml-element java-attribute="workAddresses" xml-idref="true" write-only="true">

Equivalent annotations

Employee:
@javax.xml.bind.annotation.XmlIDREF
public List<Address> workAddresses;
 
Address:
@javax.xml.bind.annotation.XmlID
public String id;

Example:

The following example will demonstrate how to configure XML collection reference mappings via XML metadata by using xml-element and xml-attribute.

org.example.Root.java

package org.example;
 
public class Root {
    public List<Employee> employees;
    public List<Address> addresses;
}

org.example.Employee.java

package org.example;
 
public class Employee {
    public List<Address> workAddresses;
 
    public List<Address> getWorkAddresses() {
        return workAddresses;
    }
 
    public void setWorkAddresses(List<Address> workAddresses) {
        this.workAddresses = workAddresses;
    }
 
}

org.example.Address.java

package org.example;
 
public class Address {
    public String id;
}

Deployment XML

<class-mapping-descriptor xsi:type="xml-class-mapping-descriptor">
  <class>org.example.Employee</class>
  <alias>Employee</alias>
  <attribute-mappings>
    <attribute-mapping xsi:type="xml-collection-reference-mapping">
      <attribute-name>workAddresses</attribute-name>
      <get-method>getWorkAddresses</get-method>
      <set-method>setWorkAddresses</set-method>
      <reference-class>org.example.Address</reference-class>
      <source-to-target-key-field-association>
        <field-reference>
          <source-field name="contact-info/work-address-id/text()" xsi:type="node"/>
          <target-field name="@aid" xsi:type="node"/>
        </field-reference>
      </source-to-target-key-field-association>
      <source-to-target-key-fields>
        <field name="contact-info/work-address-id/text()" xsi:type="node"/>
      </source-to-target-key-fields>
      <containerpolicy xsi:type="list-container-policy">
        <collection-type>java.util.Vector</collection-type>
      </containerpolicy>
      <uses-single-node>false</uses-single-node>
    </attribute-mapping>
  </attribute-mappings>
  <descriptor-type>aggregate</descriptor-type>
  <default-root-element>employee</default-root-element>
  <default-root-element-field name="employee"/>
</class-mapping-descriptor>
 
<class-mapping-descriptor xsi:type="xml-class-mapping-descriptor">
  <class>org.example.Address</class>
  <alias>Address</alias>
  <primary-key>
    <field name="@aid" xsi:type="node"/>
  </primary-key>
  <attribute-mappings>
    <attribute-mapping xsi:type="xml-direct-mapping">
      <attribute-name>id</attribute-name>
      <field name="@aid" xsi:type="node"/>
    </attribute-mapping>
  </attribute-mappings>
  <default-root-element>address</default-root-element>
  <default-root-element-field name="address"/>
</class-mapping-descriptor>

XML Instance Document

<?xml version='1.0' encoding='UTF-8'?>
<document-root>
    <employee>
        <contact-info>
            <work-address-id>a100</work-address-id>
            <work-address-id>a101</work-address-id>
        </contact-info>
    </employee>
    <addresses>
        <address aid="a100" />
        <address aid="a101" />
        <address aid="a102" />
    </addresses>
</document-root>

org/example/eclipselink-oxm.xml

This XML file demonstrates configuring XML collection reference mappings on the "org.example.Employee" class.

<?xml version="1.0" encoding="US-ASCII"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm">
    <java-types>
        <java-type name="org.example.Root">
            <xml-root-element name="document-root" />
            <java-attributes>
                <xml-element java-attribute="employees" xml-path="employee" />
                <xml-element java-attribute="addresses" xml-path="addresses/address" />
            </java-attributes>
        </java-type>
        <java-type name="org.example.Employee">
            <java-attributes>
                <xml-element java-attribute="workAddresses" xml-path="contact-info/work-address-id/text()" xml-idref="true">
                    <xml-access-methods get-method="getWorkAddresses" set-method="setWorkAddresses" />
                </xml-element>
            </java-attributes>
        </java-type>
        <java-type name="org.example.Address">
            <java-attributes>
                <xml-attribute java-attribute="id" xml-path="@aid" xml-id="true" required="true" />
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

Open Issues

This section lists the open issues that are still pending that must be decided prior to fully implementing this project's requirements.

Issue# Owner Description/Notes

Decisions

This section lists decisions made. These are intended to document the resolution of open issues or constraints added to the project that are important.

Issue# Description/Notes Decision

Copyright © Eclipse Foundation, Inc. All Rights Reserved.