1 Reply Latest reply on Jul 9, 2002 10:15 AM by andi

    Problem with one-to-many-relationship

    andi

      Hi,
      I tried to write a tree application. I wrote a NodeEJB. A Node can have m children (Nodes) and one parent (also Node).

      My problem is that I create the parent. In the next Step I try to create a child with the following code in the ejbPostCreate method of NodeBean:


      LocalNode parent = localNodeHome.findByPrimaryKey( parentId );
      LocalNode child = localNodeHome.findByPrimaryKey( id ); // this
      Collection c = parent.getChildren( );
      c.add( child );
      parent.setChildren( c );
      child.setParent( parent );

      And I always get a NullPointerException because getChildren() returns null. Next Step I tried is
      to check with an if clause if I get a null pointer and than I created a new Vector. But than I got a ClassCastException.

      I have no idea what kind of class I should use for the collection. I attach the relation from the ejb-jar.xml.

      Thanks for for help.
      Andi

      --
      <ejb-relation>
      <ejb-relation-name>node - node</ejb-relation-name>

      <ejb-relationship-role>
      <ejb-relationship-role-name>parent</ejb-relationship-role-name>
      One
      <relationship-role-source>
      <ejb-name>NodeEJB</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>parent</cmr-field-name>
      </cmr-field>
      </ejb-relationship-role>

      <ejb-relationship-role>
      <ejb-relationship-role-name>children</ejb-relationship-role-name>
      Many
      <relationship-role-source>
      <ejb-name>NodeEJB</ejb-name>
      </relationship-role-source>
      <cmr-field>
      <cmr-field-name>children</cmr-field-name>
      <cmr-field-type>java.util.Collection</cmr-field-type>
      </cmr-field>
      </ejb-relationship-role>

      </ejb-relation>