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

    How to map One-To-Many Relationship

    tdang

      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!

        • 1. Re: How to map One-To-Many Relationship
          tdang

          If it wastes a lot of your time to find out the error in the example, please send me an example of One-To-Many Relationship bean with the sql statement and its configuration files (ejb-jar.xml, jboss.xml, jbosscmp-jdbc.xml) so that I can find out the error myself.

          Thanks for your help!

          • 2. Re: How to map One-To-Many Relationship
            dsundstrom

            It is covered in the quick start guide available on our source forge download page.

            • 3. Re: How to map One-To-Many Relationship
              tdang

              I downloaded a new JBoss 3.0.1RC because some bugs with relationships were fixed. And I have studied examples as you mentioned above, I still have the problem and do not know what to do now.

              Please help me!

              Every comments are welcome!

              Regards.

              • 4. Re: How to map One-To-Many Relationship
                russellsimpkins

                I'm a little new to JBoss/EJB so excuse me if this sounds a little naive, but if you are getting, java.lang.reflect.UndeclaredThrowableException
                , I would think that the error is not in the mapping, but in the coding. Did you make sure that your code is correctly written to either declare your method as throws xException, and that in your client code is in the appropriate try catch block?