0 Replies Latest reply on Sep 13, 2001 1:36 PM by Ignacio

    Contributing: many-to-many relationships CMP 2.0

    Ignacio Newbie

      Hi all,

      AFAIK nobody has posted this before. It's the script needed in jbosscmp-jdbc.xml to implement a many-to-many relationship. I've had to surf on the code to see what (¿dain?) had implemented. If anybody more is making these things, this could prove useful. Sorry for the spanish bean names, see "Empresa" as "Company" and "Tarifa" as "Employee", for example. This is the script to map to field names in the table:

      CREATE TABLE EMPRESA_TARIFA(
       EMP_ID INT NOT NULL REFERENCES EMPRESA(ID),
       TAR_ID INT NOT NULL REFERENCES TARIFA(ID)
      );
      


       <ejb-relation>
       <ejb-relation-name>Tarifa-Empresa</ejb-relation-name>
       <table-mapping>
       <table-name>EMPRESA_TARIFA</table-name>
       <ejb-relationship-role>
       <ejb-relationship-role-name>Tarifa-has-Empresa</ejb-relationship-role-name>
       <table-key-fields>
       <table-key-field>
       <field-name>id</field-name>
       <column-name>EMP_ID</column-name>
       </table-key-field>
       </table-key-fields>
       </ejb-relationship-role>
      
       <ejb-relationship-role>
       <ejb-relationship-role-name>Tarifa-belongsto-Empresa</ejb-relationship-role-name>
       <table-key-fields>
       <table-key-field>
       <field-name>id</field-name>
       <column-name>TAR_ID</column-name>
       </table-key-field>
       </table-key-fields>
       </ejb-relationship-role>
       </table-mapping>
       </ejb-relation>
      


      The ejb-jar equivalent, according to the EJB 2.0 spec, is:

       <ejb-relation>
       <ejb-relation-name>Tarifa-Empresa</ejb-relation-name>
      
       <ejb-relationship-role>
       <ejb-relationship-role-name>Tarifa-has-Empresa</ejb-relationship-role-name>
       <multiplicity>Many</multiplicity>
       <relationship-role-source>
       <ejb-name>Tarifa</ejb-name>
       </relationship-role-source>
       <cmr-field>
       <cmr-field-name>empresas</cmr-field-name>
       <cmr-field-type>java.util.Collection</cmr-field-type>
       </cmr-field>
       </ejb-relationship-role>
      
       <ejb-relationship-role>
       <ejb-relationship-role-name>Tarifa-belongsto-Empresa</ejb-relationship-role-name>
       <multiplicity>Many</multiplicity>
       <relationship-role-source>
       <ejb-name>Empresa</ejb-name>
       </relationship-role-source>
       <cmr-field>
       <cmr-field-name>tarifas</cmr-field-name>
       <cmr-field-type>java.util.Collection</cmr-field-type>
       </cmr-field>
       </ejb-relationship-role>
       </ejb-relation>
      


      I've wasted, er, inverted :), half the morning to get this. I'm posting this in the hope that helps somebody in the same situation as I did. Regards.