Show TOC Start of Content Area

Function documentation One-to-One Bidirectional Relationships  Locate the document in its SAP Library structure

Use

In a one-to-one bidirectional relationship, there are corresponding fields of persistence capable class type on both sides of the relationship. The field type is the type of the persistence capable class on the other side.

In the relational model, there is a foreign key on one of the sides. There must be a unique constraint for the foreign key column.

Example

There is a one-to-one relationship between the class example.jdo.Manager and class example.jdo.Department (each department has a single manager and each manager is a head of one department only). Both classes have a relationship field ref of persistence capable class type. Class Manager is mapped to table TMP_MANAGER, and class Department is mapped to table TMP_DEPARTMENT. In table TMP_DEPARTMENT there is a foreign key column MANAGER with primary key column MANID in table TMP_MANAGER. In the mapping metadata there is a foreign key element for both relationship fields ref containing the same foreign key (identified by the foreign key name). The update property in the relationship-field element must be set to false for the relationship field that does not have the foreign key columns in its classes tables. In this example, this is the relationship field ref of class Manager.

 

This graphic is explained in the accompanying text

 

The XML metadata has the following contents:

 

<jdo>

  <package name="example.jdo">

    <class name="Manager" identity-type="application" objectid-class="Manager$Id">

      <field name="manid" persistence-modifier="persistent" primary-key="true"/>

      <field name="ref" persistence-modifier="persistent"

        embedded="false" default-fetch-group="false"/>

    </class>

    <class name="Department" identity-type="application" objectid-class="Department$Id">

      <field name="depid" persistence-modifier="persistent" primary-key="true"/>

      <field name="ref" persistence-modifier="persistent"

        embedded="false" default-fetch-group="false"/>

    </class>

  </package>

</jdo>

 

<map version="1.0">

  <package name="example.jdo">

    <class name="Manager">

      <field name="manid">

        <column name="MANID" table="TMP_MANAGER"/>

      </field>

      <relationship-field name="ref" multiplicity="one" update="false">

        <foreign-key name="DEPARTMENT_TO_MANAGER"

            foreign-key-table="TMP_DEPARTMENT"

            primary-key-table="TMP_MANAGER">

          <column-pair foreign-key-column="MANAGER" primary-key-column="MANID"/>

        </foreign-key>

      </relationship-field>

    </class>

    <class name="Department">

      <field name="depid">

        <column name="DEPID" table="TMP_DEPARTMENT"/>

      </field>

      <relationship-field name="ref" multiplicity="one">

        <foreign-key name="DEPARTMENT_TO_MANAGER"

            foreign-key-table="TMP_DEPARTMENT"

            primary-key-table="TMP_MANAGER">

          <column-pair foreign-key-column="MANAGER" primary-key-column="MANID"/>

        </foreign-key>

      </relationship-field>

    </class> 

  </package>

</map>

 

 

End of Content Area