4 Replies Latest reply on Oct 1, 2002 6:10 AM by morten Matras

    How to use priamry key field as foreign key field

    tsangcn Newbie

      Hello

      I am new to CMP 2.0 and CMR.
      I have some problem in modelling a simple bidirectional 1-1 relationship.

      I have 2 tables USER_TABLE and USER_INFO_TABLE as follows

      USER_TABLE has 2 columns - userid, password
      USER_INFO_TABLES has 3 columns - userid, name, address

      where userid servers as primary key and foreign key for the two tables.

      ejb-jar.xml
      -----------

      <ejb-relation>
      <ejb-relation-name>user-userinfo</ejb-relation-name>
      <!-- bidirectional -->
      <ejb-relationship-role>
      <ejb-relationship-role-name>user-has-userinfo</ejb-relationship-role-name>
      One
      <relationship-role-source>
      <ejb-name>UserEJB</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>userInfo</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>userinfo-part-of-user</ejb-relationship-role-name>
      One
      <cascade-delete />
      <relationship-role-source>
      <ejb-name>UserInfoEJB</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>user</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>
      </ejb-relation>


      jbosscmp-jdbc.xml
      -----------------

      <ejb-relation>
      <ejb-relation-name>user-has-userinfo</ejb-relation-name>
      <foreign-key-mapping/>
      <ejb-relationship-role>
      <ejb-relationship-role-name>user-has-userinfo</ejb-relationship-role-name>
      <fk-constraint>false</fk-constraint>
      <key-fields>
      <key-field>
      <field-name>userid</field-name>
      <column-name>USER_USERID</column-name>
      </key-field>
      </key-fields>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>userinfo-part-of-user</ejb-relationship-role-name>
      <fk-constraint>false</fk-constraint>
      <key-fields>
      </key-fields>
      </ejb-relationship-role>
      </ejb-relation>


      The database I used is Firebird.
      It works OK, but JBoss will insert a column USER_USERID in USER_INFO_TABLE as foreign key.
      How to get rid of it and use USERID as the foreign key ?
      I have try to put USERID as column-name in the key-fields, but Firebird complaint the column already exist.
      I have try remove the keyfield from user-has-userinfo, but JBoss need to specify at least one key-field in a 1-1 relationship.

      Thanks
      CN