Show TOC Start of Content Area

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

Use

In a many-to-many bidirectional relationship, there are corresponding fields of type java.util.Set on both sides of the relationship. The element type of the sets is the persistence capable class type of the other side.

In the relational model, there is a join table that contains the foreign keys of both sides.

 

Example

There is a many-to-many relationship between the class example.jdo.Course and class example.jdo.Student (many students may visit each course, and each student may choose several courses). Both classes have a relationship field set of type java.util.Set, which contains elements of the persistence capable class type of the other side. Class Course is mapped to table TMP_COURSE; and class Student is mapped to table TMP_STUDENT. There is a join table TMP_JOIN with foreign key columns COURSE and STUDENT. Column COURSE is the foreign key column for the primary key CID in table TMP_COURSE, and column STUDENT  is the foreign key column for the primary key SID in table TMP_STUDENT. In the mapping metadata, there are two foreign-key elements for both relationship fields set containing the same two foreign keys: one foreign key from the join table to the table of the referring class type, and another foreign key from the join table to the table of the referred class type. In one relationship field element, the property update must be set to false.

 

This graphic is explained in the accompanying text

 

The XML metadata has the following contents:

 

<jdo>

  <package name="example.jdo">

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

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

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

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

        <collection element-type="Student" embedded-element="false"/>

      </field>

    </class>

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

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

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

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

        <collection element-type="Course" embedded-element="false"/>

      </field>

    </class>

  </package>

</jdo>

 

<map version="1.0">

  <package name="example.jdo">

    <class name="Course">

      <field name="cid">

        <column name="CID" table="TMP_COURSE"/>

      </field>

      <relationship-field name="set" multiplicity="many" join-table="true">

        <foreign-key name="JOIN_TO_COURSE"

            foreign-key-table="TMP_JOIN" primary-key-table="TMP_COURSE">

          <column-pair foreign-key-column="COURSE" primary-key-column="CID"/>

        </foreign-key>

        <foreign-key name="JOIN_TO_STUDENT"

            foreign-key-table="TMP_JOIN" primary-key-table="TMP_STUDENT">

          <column-pair foreign-key-column="STUDENT" primary-key-column="SID"/>

        </foreign-key>

      </relationship-field>

    </class>

    <class name="Student">

      <field name="sid">

        <column name="SID" table="TMP_STUDENT"/>

      </field>

      <relationship-field name="set" multiplicity="many" join-table="true" update="false">

        <foreign-key name="JOIN_TO_STUDENT"

            foreign-key-table="TMP_JOIN" primary-key-table="TMP_STUDENT">

          <column-pair foreign-key-column="STUDENT" primary-key-column="SID"/>

        </foreign-key>

        <foreign-key name="JOIN_TO_COURSE"

            foreign-key-table="TMP_JOIN" primary-key-table="TMP_COURSE">

          <column-pair foreign-key-column="COURSE" primary-key-column="CID"/>

        </foreign-key>

      </relationship-field>

    </class> 

  </package>

</map>

 

End of Content Area