0 Replies Latest reply on Nov 10, 2004 2:50 PM by kornel csaszar

    CMP/CMR Why do relationship keys have to be primary keys of

    kornel csaszar Newbie

      Hi All,

      I have three tables. TableA, TableB, and a M:M link between them TableA_B. The link table has a record
      for each association of a TableA and TableB record. This is a typical many-to-many relational problem.
      The association table has one-to-many ralationship with both TableA and TableB.

      Question 1:
      ----------

      It seems that the keyfields in each relationship role must be primary keys of the associated entity on
      that side of the relationship. In my case (and in most real life cases) the relationship table has two foreign
      keys, one pointing to each associated table. So the two keys are not primary keys in the relationship table. Why
      is JBoss expecting TableB_ID (see below in the key-fields entry) to be a primary key of TableA_B
      It throws the following exception because TableBrecs (see error below) is not a field which is associated with a primary key
      of the association table. (Of course it isn't. It should be a foreign key! What am I missing?)

      20:23:50,347 INFO [EjbModule] Deploying TableA_EJB
      20:23:50,728 INFO [EjbModule] Deploying TableB_EJB
      20:23:50,758 INFO [EjbModule] Deploying TableA_B_EJB
      20:23:51,389 ERROR [EntityContainer] Starting failed jboss.j2ee:jndiName=TableA_EJB,service=EJB
      org.jboss.deployment.DeploymentException: Role 'TableA_B-has-many-TableBrecs' on Entity Bean 'TableA_B_EJB' : CMP field for
      key not found: field name='TableBrecs'
      at org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCRelationshipRoleMetaData.loadKeyFields(JDBCRelationshipRoleMetaD
      ta.java:423)
      at org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCRelationshipRoleMetaData.init(JDBCRelationshipRoleMetaData.java:
      84)
      at org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCRelationMetaData.(JDBCRelationMetaData.java:354)
      at org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCApplicationMetaData.(JDBCApplicationMetaData.java:471)

      Question 2:
      -----------
      Do I need to declare the cmr fields in either ejb-jar.xml or jbosscmp-jdbc.xml as cmp fields as well? Presently I
      only list them in the relationships section as show below:

      ejb-jar.xml

      <ejb-relation>
      <ejb-relation-name>TableA_B-TableB</ejb-relation-name>
      <ejb-relationship-role>
      <ejb-relationship-role-name>TableA_B-has-many-TableBs</ejb-relationship-role-name>
      One
      <relationship-role-source><ejb-name>TableA_B_EJB</ejb-name></relationship-role-source>
      <cmr-field>
      <cmr-field-name>TableBrecs</cmr-field-name>
      <cmr-field-type>java.util.Set</cmr-field-type>
      </cmr-field>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>TableB-associated-with-a-TableATableB</ejb-relationship-role-name>
      Many
      <relationship-role-source><ejb-name>TableB_EJB</ejb-name></relationship-role-source>
      </ejb-relationship-role>
      </ejb-relation>
      <ejb-relation>
      <ejb-relation-name>TableA_B-TableA</ejb-relation-name>
      <ejb-relationship-role>
      <ejb-relationship-role-name>TableA_B-has-many-TableAs</ejb-relationship-role-name>
      One
      <relationship-role-source><ejb-name>TableA_B_EJB</ejb-name></relationship-role-source>
      <cmr-field>
      <cmr-field-name>TableArecss</cmr-field-name>
      <cmr-field-type>java.util.Set</cmr-field-type>
      </cmr-field>
      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>TableA-associated-with-a-TableA_B</ejb-relationship-role-name>
      Many
      <relationship-role-source><ejb-name>TableA_EJB</ejb-name></relationship-role-source>
      </ejb-relationship-role>
      </ejb-relation>


      jbosscmp-jdbc.xml

      <ejb-relation>
      <ejb-relation-name>TableATableB-TableB</ejb-relation-name>
      <ejb-relationship-role>
      <ejb-relationship-role-name>TableATableB-has-many-TableBs</ejb-relationship-role-name>
      <key-fields>
      <key-field>
      <field-name>TableBrecs</field-name>
      <column-name>TableB_ID</column-name>
      </key-field>
      </key-fields>

      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>TableB-associated-with-a-TableA_B</ejb-relationship-role-name>
      <key-fields/>
      </ejb-relationship-role>
      </ejb-relation>
      <ejb-relation>
      <ejb-relation-name>TableATableB-TableA</ejb-relation-name>
      <ejb-relationship-role>
      <ejb-relationship-role-name>TableATableB-has-many-TableAs</ejb-relationship-role-name>
      <key-fields>
      <key-field>
      <field-name>TableArecs</field-name>
      <column-name>TableA_ID</column-name>
      </key-field>
      </key-fields>

      </ejb-relationship-role>
      <ejb-relationship-role>
      <ejb-relationship-role-name>TableA-associated-with-a-TableA_B</ejb-relationship-role-name>
      <key-fields/>
      </ejb-relationship-role>
      </ejb-relation>