3 Replies Latest reply on May 1, 2004 3:21 PM by sesques

    Many-to-many relation: How is my ejb container aware of a ta

    balteo

      Hello all,

      Further to several posts on this forum, I am still struggling with the same many-to-many ejb relation and before rushing into coding and possibly running into big problems I would like to understand a few points about CMR and CMP.

      I have three tables (as before) which are mapped to two ejbs as follows:

      *First table, called "establishments", contains two fiels "db_establishment_id" and "db_establishment_name".
      Here is a sample:

      db_establishment_id(pk) db_establishment_name
      ---------------------------------------------
      1 The black frog
      2 The king's head
      3 McDonalds
      


      *Second table, called "categories", contains two fields "db_category_id" and "db_category_name"
      Here is a sample:
      db_category_id(pk) db_category_name
      -----------------------------------
      1 restaurant
      2 pub
      



      *Third table, called "establishments_categories", contains two fields "db_establishment_id" and "db_id_category"
      Here is a sample:
      db_establishment_id(pk,fk) db_category_id(pk,fk)
      --------------------------------------
      1 2
      2 2
      2 1
      3 1
      


      These THREE tables map to TWO ejbs in a many-to-many bi-di relation.


      My question is as follows:

      How is my ejb container aware of a table not mentioned anywhere (here establishments_categories)? How does it update or read from this table if it is not mentioned anywhere in the DDs? Do I need to mention it in my jboss DD? Can you give me a sample of that jboss DD?

      Thanks in advance,

      Julien Martin.

        • 1. Re: Many-to-many relation: How is my ejb container aware of
          sesques

          Hi Julien,

          In JBoss, the mapping is always done in jbosscmp-jdbc.xml
          In this file, your relation should be something like;

           <ejb-relation>
           <ejb-relation-name>Etablishment-Category</ejb-relation-name>
           <relation-table-mapping>
           <table-name>establishments_categories</table-name>
           <datasource>Your data source</datasource>
           <datasource-mapping>Your data source mapping</datasource-mapping>
           <create-table>What you want</create-table>
           <remove-table>What you want</remove-table>
           </relation-table-mapping>
          
           <ejb-relationship-role>
           <!-- Etablishment side -->
           <ejb-relationship-role-name>Etablishment-have-Category</ejb-relationship-role-name>
           <key-fields>
           <key-field>
           <field-name>etablishment_id</field-name>
           <column-name>db_establishment_id</column-name>
           </key-field>
           </key-fields>
          
           </ejb-relationship-role>
           <ejb-relationship-role>
           <!-- Category side -->
           <ejb-relationship-role-name>Category-belongs-to-Etablishment</ejb-relationship-role-name>
           <key-fields>
           <key-field>
           <field-name>category_id</field-name>
           <column-name>db_category_id</column-name>
           </key-field>
           </key-fields>
          
           </ejb-relationship-role>
           </ejb-relation>
          


          Something important: You must repeat the datasource and datasource mapping in the relation-table-mapping if you don't set it in the default section.

          I suggest you using XDoclet to generate all the descriptors.



          • 2. Many-to-many relation.
            balteo

            Thanks a lot for your reply Pascal,
            Does Xdoclet work for ejb 2.1??
            Julien.

            • 3. Re: Many-to-many relation: How is my ejb container aware of
              sesques

              Hi Julien,

              The current release 1.2 (I use the beta 3 myself) NO.
              EJB 2.1 will be supported by XDoclet 2 but it is not yet relased.
              Check the project at:http://xdoclet.codehaus.org/

              Anyway, EJB 2.1 will be supported only on JBoss 4.x.x version, which is not yet released.