4 Replies Latest reply on Aug 3, 2002 11:46 AM by Russell Simpkins

    How to map One-To-Many Relationship

    Tung Dang Novice

      I have the following database structure:

      table product:
      - productid char(10)
      - description varchar(100)

      table detail:
      - id char(10)
      - prodid char(10)
      - description varchar(100)

      One product can have many details. The field prodid in the table detail is the foreign key of product.

      I have 2 beans: ProductBean and DetailBean. In the ProductBean I have the get/set methods for relationship like this:

      public abstract Collection getDetails();
      public abstract void setDetails(Collection details);

      Since I have the unidirectional relationship, I do not have the get/set method for relationship in the DetailBean. I declared ralationship in ejb-jar.xml as well.

      In ejb-jar.xml, I do not declare the field prodid of the table.

      Here is my mapping in the jbosscmp-jdbc.xml


      <ejb-relation>
      <ejb-relation-name>Product-Detail</ejb-relation-name>
      <foreign-key-mapping/>
      <ejb-relationship-role>
      <ejb-relationship-role-name>product-has-details</ejb-relationship-role-name>
      <fk-constraint>false</fk-constraint>

      <key-fields>
      <key-field>
      <field-name>productID</field-name>
      <column-name>prodid</column-name>
      </key-field>
      </key-fields>
      </ejb-relationship-role>

      <ejb-relationship-role>
      <ejb-relationship-role-name>details-belogs-to-product</ejb-relationship-role-name>
      <key-fields/>
      </ejb-relationship-role>
      </ejb-relation>


      I could successfully deployed, but while running the client, I got the following error:

      java.lang.reflect.UndeclaredThrowableException

      The code of the client looks like this:
      ...
      ProductHome home = getHome();
      Product r = home.findByPrimaryKey("pr1");
      Collection details = r.getDetails();
      ...
      My question is: Is my mapping correct? What does the error mean?

      Thanks!