Skip Headers

Oracle TopLink Developer's Guide 10g Release 3 (10.1.3) B13593-01 |
| Home | Solution Area | Contents | Index

Previous | Next |

# XML Composite Object Mapping

XML composite object mappings represent a relationship between two classes. In XML, the "owned" class may be nested with the element tag representing the "owning" class. You can use a composite object XML mapping in the following scenarios:

- Mapping Into the Parent Record

- Mapping to an Element

- Mapping to Different Elements by Element Name

- Mapping to Different Elements by Element Position

See Chapter 69, "Configuring an XML Composite Object Mapping" for more information.

## Mapping Into the Parent Record

The composite object may be mapped to the same record as the parent.

Note: The nodes mapped to by the composite object must be sequential.

Given the XML schema in Example 65-40, Figure 65-23 illustrates an XML composite object mapping into the parent record in a corresponding XML document. Example 65-41 shows how to configure this mapping in Java.

Example 65-40 Schema for XML Composite Object Mapping into the Parent Record

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <xsd:element name="customer" type="customer-type"/>
        <xsd:complexType name="customer-type">
            <xsd:sequence>
                <xsd:element name="first-name" type="xsd:string"/>
                <xsd:element name="last-name" type="xsd:string"/>
                <xsd:element name="street" type="xsd:string"/>
                <xsd:element name="city" type="xsd:string"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:schema>

Figure 65-23 XML Composite Object Mapping into the Parent Record

Description of "Figure 65-23 XML Composite Object Mapping into the Parent Record"

Example 65-41 Java for XML Composite Object Mapping into the Parent Record

    XMLCompositeObjectMapping addressMapping = new XMLCompositeObjectMapping();
    addressMapping.setAttributeName("address");
    addressMapping.setXPath(".");
    addressMapping.setReferenceClass(Address.class);

## Mapping to an Element

Given the XML schema in Example 65-42, Figure 65-24 illustrates an XML composite object mapping to an element in a corresponding XML document. Example 65-43 shows how to configure this mapping in Java.

Example 65-42 Schema for XML Composite Object Mapping to an Element

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <xsd:element name="customer" type="customer-type"/>
        <xsd:complexType name="customer-type">
            <xsd:sequence>
                <xsd:element name="first-name" type="xsd:string"/>
                <xsd:element name="last-name" type="xsd:string"/>
                <xsd:element name="address">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="street" type="xsd:string"/>
                            <xsd:element name="city" type="xsd:string"/>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:schema>

Figure 65-24 XML Composite Object Mapping to an Element

Description of "Figure 65-24 XML Composite Object Mapping to an Element"

Example 65-43 Java for XML Composite Object Mapping to an Element

    XMLCompositeObjectMapping addressMapping = new XMLCompositeObjectMapping();
    addressMapping.setAttributeName("address");
    addressMapping.setXPath("address");
    addressMapping.setReferenceClass(Address.class);

## Mapping to Different Elements by Element Name

An object may have multiple composite object mappings to the same reference class. Each composite object mapping must have a unique XPath. This example uses unique XPaths by name .

Given the XML schema in Example 65-44, Figure 65-25 illustrates an XML composite object mapping to different elements by name in a corresponding XML document. Example 65-45 shows how to configure this mapping in Java.

Example 65-44 Schema for XML Composite Object Mapping to Elements by Name

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <xsd:element name="customer" type="customer-type"/>
        <xsd:complexType name="customer-type">
            <xsd:sequence>
                <xsd:element name="first-name" type="xsd:string"/>
                <xsd:element name="last-name" type="xsd:string"/>
                <xsd:element name="billing-address" type="address-type"/>
                <xsd:element name="shipping-address" type="address-type"/>
            </xsd:sequence>
        </xsd:complexType>
        <xsd:complexType name="address-type">
            <xsd:sequence>
                <xsd:element name="street" type="xsd:string"/>
                <xsd:element name="city" type="xsd:string"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:schema>

Figure 65-25 XML Composite Object Mapping to Elements by Name

Description of "Figure 65-25 XML Composite Object Mapping to Elements by Name"

Example 65-45 Java for XML Composite Object Mapping to Elements by Name

    XMLCompositeObjectMapping billingAddressMapping = new XMLCompositeObjectMapping();
    billingAddressMapping.setAttributeName("billingAddress");
    billingAddressMapping.setXPath("billing-address");
    billingAddressMapping.setReferenceClass(Address.class);

    XMLCompositeObjectMapping shippingAddressMapping = new XMLCompositeObjectMapping();
    shippingAddressMapping.setAttributeName("shippingAddress");
    shippingAddressMapping.setXPath("shipping-address");
    shippingAddressMapping.setReferenceClass(Address.class);

## Mapping to Different Elements by Element Position

An object may have multiple composite object mappings to the same reference class. Each composite object mapping must have a unique XPath. This example uses unique XPaths by position.

Given the XML schema in Example 65-44, Figure 65-25 illustrates an XML composite object mapping to different elements by position in a corresponding XML document. Example 65-45 shows how to configure this mapping in Java.

Example 65-46 Schema for XML Composite Object Mapping to Elements by Position

    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <xsd:element name="customer" type="customer-type"/>
        <xsd:complexType name="customer-type">
            <xsd:sequence>
                <xsd:element name="first-name" type="xsd:string"/>
                <xsd:element name="last-name" type="xsd:string"/>
                <xsd:element name="address" maxOccurs="2">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="street" type="xsd:string"/>
                            <xsd:element name="city" type="xsd:string"/>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:schema>

Figure 65-26 XML Composite Object Mapping to Elements by Position

Description of "Figure 65-26 XML Composite Object Mapping to Elements by Position"

Example 65-47 Java for XML Composite Object Mapping to Elements by Position

    XMLCompositeObjectMapping billingAddressMapping = new XMLCompositeObjectMapping();
    billinAddressMapping.setAttributeName("billingAddress");
    billinAddressMapping.setXPath("address[1]");
    billinAddressMapping.setReferenceClass(Address.class);

    XMLCompositeObjectMapping shippingAddressMapping = new XMLCompositeObjectMapping();
    shippingAddressMapping.setAttributeName("shippingAddress");
    shippingAddressMapping.setXPath("address[2]");
    shippingAddressMapping.setReferenceClass(Address.class);