Skip Headers

# EIS One-to-Many Mappings With Key on Source

Previous

Next

Figure: EIS One-to-Many Mapping with Key on Source illustrates an EIS one-to-many mapping between the `Employee` class attribute `projects` and multiple `Project` class instances using XML records in a key on source design.

EIS One-to-Many Mapping with Key on Source

Description of "EIS One-to-Many Mapping with Key on Source"

When a read interaction is executed on the `Employee` object, TopLink puts each target `Project` object's foreign key into the `Employee` record as a subelement. If you specify only one pair of source and target XML fields, by default, the foreign keys are not grouped in the `Employee` record. If you specify more than one pair of source and target XML fields, you must choose a grouping element (see "Configuring Reference Descriptors"). Figure: EIS One-to-Many Mapping with Key on Source shows an `Employee` record with grouping element `Project`. TopLink then uses the selection interaction you configure on the `Employee` descriptor to retrieve the appropriate instances of `Project` and creates a record for each in the `Employee` object's transaction. In this example, you can designate the `Project` class's read interaction as the selection interaction.

The general procedure for creating and configuring this mapping is as follows:

1. Create a one-to-many EIS mapping on `Employee` attribute `project`.

2. Configure the reference descriptor as `Project` (see "Configuring Reference Descriptors").

3. Configure the source and target foreign keys (see "Configuring Foreign Key Pairs").

   In this example:

   - Source XML Field: `PROJECT`

   - Target XML Field: `@ID`

4. Configure the selection interaction (see "Configuring Selection Interaction").

   In this example, you can designate the `Project` class's read interaction as the selection interaction.

Given the XSD shown in Example: XML Schema for EIS One-to-One Mapping with Key on Target, you can configure an EIS one-to-many mapping with key on source, as Example: EIS One-to-One Mapping with Key on Target shows. In this case, the source object contains a foreign key reference to the target object. In the following example, the source object is `Employee`, and the target object is `Project`. Here, the `Employee` object has one or more `Project` instances that are referenced by `Project` id.

XML Schema for EIS One-to-Many Mapping with Key on Source

    <?xml version="1.0"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        elementFormDefault="qualified"
        attributeFormDefault="unqualified">
        <xsd:element name="employee" type="employee-type"/>
        <xsd:element name="project" type="project-type"/>
        <xsd:complexType name="employee-type">
            <xsd:sequence>
                <xsd:element name="name" type="xsd:string"/>
                <xsd:element name="projects">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="project-id"
                                type="xsd:integer" maxOccurs="unbounded"/>
                        </xsd:sequence>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
        <xsd:complexType name="project-type">
            <xsd:sequence>
                <xsd:element name="id" type="xsd:integer"/>
                <xsd:element name="leader" type="xsd:string"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:schema>

EIS One-to-Many Mapping with Key on Source

    // Employee descriptor
    EISDescriptor descriptor = new EISDescriptor();
    descriptor.setJavaClass(Employee.class);
    descriptor.setDataTypeName("employee");
    descriptor.setPrimaryKeyFieldName("name/text()");

    EISOneToManyMapping projectMapping = new EISOneToManyMapping();
    projectMapping.setReferenceClass(Project.class);
    projectMapping.setAttributeName("projects");
    projectMapping.setForeignKeyGroupingElement("projects"); projectMapping.setIsForeignKeyRelationship(true); projectMapping.dontUseIndirection();
    projectMapping.addForeignKeyFieldName("project-id/text()", "id/text()");