Skip Headers

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

Previous

Next

Figure: EIS One-to-One Mapping with Key on Target 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 target design. You still configure a one-to-one EIS mapping between `Employee` and `Project` but in this design, the `Project` attribute `leader` contains the foreign key of the `Employee` object.

EIS One-to-Many Mapping with Key on Target

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

When a read interaction is executed on the `Employee` object, TopLink 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, the `Project` class's read interaction is unlikely to be sufficient: it is likely implemented to read based on `Project` attribute `Id`, not on `leader`. If this is the case, you must define a separate selection interaction on the `Employee` descriptor that does the following: finds the `Project`, whose `leader` equals X, where X is "Jane".

Note that in this configuration, `Project` attribute `leader` is not persisted. If you want this attribute persisted, you must configure a one-to-one EIS mapping from it to `Employee` attribute `firstName`.

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

1. Create a one-to-one 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, you select Foreign Keys Located On Source and specify one pair of source and target XML fields:

   - Source XML Field:

   - Target XML Field:

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

   In this example, you must define a separate selection interaction on the `Employee` descriptor.

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 target as Example: EIS One-to-One Mapping with Key on Target shows. In this case, the target object contains a foreign key reference to the source object. In the following example, the source object is `Employee`, and the target object is `Project`. Here, each `Project` references its `leader` using the employee's name.

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

    <?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 Target

    // Project descriptor
    EISDescriptor descriptor = new EISDescriptor();
    descriptor.setJavaClass(Project.class);
    descriptor.setDataTypeName("project");
    descriptor.setPrimaryKeyFieldName("id/text()");

    EISOneToManyMapping leaderMapping = new EISOneToOneMapping();
    leaderMapping.setReferenceClass(Employee.class);
    leaderMapping.setAttributeName("leader");
    leaderMapping.dontUseIndirection();
    leaderMapping.addForeignKeyFieldName("leader/text()", "name/text()");