1 Reply Latest reply on Oct 6, 2004 3:17 PM by bigdaddy

    How do I relate foreign key entity with relation table mappi

    bigdaddy

      Using JBoss 3.2.5
      --------------------

      I have a CMR relationship using a foreign key mapping and a CMR relationship with a relation table mapping:

      (1) customer -- (MANY) person (person contains customer primary key as foreign key)
      (1) person -- (MANY) schedule (person and schedule use a relation table that consists of their primary keys)

      So Person essentially has a compound primary key (person_id and customer_id) since it is using a foreign key CMR relationship. But, I also need a CMR relationship between Person and Schedule, in which case the person_schedule relation table would contain person_id, customer_id, and schedule_id as its primary key. How can I accomplish this with CMR?

      What are the entity beans and jbossjdbc-cmp.xml supposed to look like?

      I currently have:

      public abstract class PersonEJB implements EntityBean {

      public java.lang.Integer ejbCreate (CustomerEJBLocal newCustomer) throws CreateException
      {
      return null;
      }

      public void ejbPostCreate (CustomerEJBLocal newCustomer) throws CreateException
      {
      setCustomer(newCustomer);
      }

      ...

      }

      public abstract class ScheduleEJB implements EntityBean
      {
      public java.lang.Integer ejbCreate (PersonEJBLocal newPerson) throws CreateException
      {
      return null;
      }

      public void ejbPostCreate (PersonEJBLocal newPerson) throws CreateException
      {
      setPerson(newPerson);
      }

      ...

      }

      jbossjdbc-cmp.xml:

      <ejb-relation>
      <ejb-relation-name>Person-Schedule</ejb-relation-name>
      <relation-table-mapping>
      <table-name>person_schedule</table-name>
      <create-table>false</create-table>
      </relation-table-mapping>

      <ejb-relationship-role>
      <ejb-relationship-role-name>Schedule-belongs-to-Person</ejb-relationship-role-name>
      <key-fields>
      <key-field>
      <field-name>id</field-name>
      <column-name>schedule_id</column-name>
      </key-field>
      </key-fields>

      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>Person-has-Schedule</ejb-relationship-role-name>
      <key-fields>
      <key-field>
      <field-name>id</field-name>
      <column-name>person_id</column-name>
      </key-field>
      </key-fields>

      </ejb-relationship-role>
      </ejb-relation>


      The problem is where do I include the customer_id for person within this relation between person and schedule?

      Thanks.

        • 1. Re: How do I relate foreign key entity with relation table m
          bigdaddy

          To make this work will I have to explicitly create a customerId field in the Person entity bean and set the customerId within ejbPostCreate like this:

          public abstract class PersonEJB implements EntityBean
          {
          public void ejbPostCreate (CustomerEJBLocal newCustomer) throws CreateException
          {
          setCustomer(newCustomer);
          setCustomerId(newCustomer.getCustomerId());
          }

          ...

          }

          Will I be able to add customer_id to jbosscmp-jdbc.xml like this:

          <ejb-relation>
          <ejb-relation-name>Person-Schedule</ejb-relation-name>
          <relation-table-mapping>
          <table-name>person_schedule</table-name>
          <create-table>false</create-table>
          </relation-table-mapping>

          <ejb-relationship-role>
          <ejb-relationship-role-name>Schedule-belongs-to-Person</ejb-relationship-role-name>
          <key-fields>
          <key-field>
          <field-name>id</field-name>
          <column-name>schedule_id</column-name>
          </key-field>
          </key-fields>

          </ejb-relationship-role>
          <ejb-relationship-role>
          <ejb-relationship-role-name>Person-has-Schedule</ejb-relationship-role-name>
          <key-fields>
          <key-field>
          <field-name>id</field-name>
          <column-name>person_id</column-name>
          </key-field>
          <key-field>
          <field-name>customerId</field-name>
          <column-name>customer_id</column-name>
          </key-field>
          </key-fields>

          </ejb-relationship-role>
          </ejb-relation>

          Any assistance is appreciated.

          Thanks.