Skip Headers

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

Previous

Next

Figure: EIS One-to-One Mapping with Key on Target illustrates EIS one-to-one mapping between the `Employee` class attribute `project` and the `Project` class 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-One Mapping with Key on Target

Description of "EIS One-to-One 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 instance of `Project` and creates a record for it 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 the value of `Employee` attribute `firstName`.

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:

   - Source XML Field: `firstName/text()`

   - Target XML Field: `leader/text()`

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-one 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, a `Project` references its `leader` using the employee's name.

XML Schema for EIS One-to-One 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="project">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="project-id" type="xsd:integer"/>
                        </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-One Mapping with Key on Target

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

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