Container Managed Relationships in a one-to-one (1-1) context.

In this example, the Teacher CMP bean is related to the Course CMP bean. At Hogwarts, teaching a course is so demanding that every teacher can teach only one Course. Therefore this is a one-to-one relation.

We can have Resin-CMP manage this relationship for us so that we don't have to use JDBC to find related entities. Our two beans in this example will use the following 2 tables:

    # this table stores the teacher entities
    CREATE TABLE one2one_teacher (
      name VARCHAR(250) NOT NULL,
      course VARCHAR(250) NOT NULL,

      PRIMARY KEY(name)
    );

    # this table stores the course entities
    CREATE TABLE one2one_course(
      name VARCHAR(250) NOT NULL,
      room VARCHAR(250) NOT NULL,
      teacher VARCHAR(250) NOT NULL,

      PRIMARY KEY(name)
    );

    

Resin-CMP can find the course associated with a teacher by looking for a 'name' in the one2one_course table that matches the 'course' in a Teacher entity.
Conversely, Resin-CMP can find the teacher associated with a a course by looking for a 'name' in the one2one_teacher table that matches the 'teacher' in the Course entity.

Setting up the deployment descriptor

All setup related to CMR is done in the <relationships> section. We set up our one2one relationship like this:

 <relationships>
  <ejb-relation>
   <ejb-relationship-role>
    <relationship-role-source>
      <ejb-name>one2one_teacher</ejb-name>
    </relationship-role-source>
    <cmr-field>
     <cmr-field-name>course</cmr-field>
    <multiplicity>One</multiplicity>
   </ejb-relationship-role>
   <ejb-relationship-role>
    <relationship-role-source>
      <ejb-name>one2one_course</ejb-name>
    </relationship-role-source>
    <cmr-field>
     <cmr-field-name>teacher</cmr-field>
    <multiplicity>One</multiplicity>
    </ejb-relationship-role>
  </ejb-relation>
 </relationships>

Every <ejb-relation> has two participating entity beans. Their role in the relation is defined within a <ejb-relationship-role> section. Use the <ejb-name> tag to describe which entity the <ejb-relationship-role> block belongs to.

Writing code for the CMR fields

For the Course Bean, we have defined teacher as a CMR field. We need to match this with two method declarations in CourseBean.java. Resin-CMP will automatically implement these methods when the bean is deployed.


    abstract public Teacher getTeacher();
    abstract public void setTeacher(Teacher teacher); // doesn't work in 06-20 snap
    

We can use the setTeacher method to assign a teacher to a Course. If the Course is already being taught by another teacher, Resin-CMP will disassociate the original teacher from the Course.

Similarly, for the Teacher Bean, we have defined course as a CMR field. We need to match this with a method declaration in TeacherBean.java:


    abstract public Course getCourse();
    abstract public void setCourse(Course course); // doesn't work in 06-20 snap
    

Here we can use the setCourse method to assign a Course to a Teacher. If the Teacher is currently teaching another Course, Resin-CMP will disassociate the original Course from the teacher.