1 Reply Latest reply on Aug 22, 2002 4:51 PM by David Ward

    CMR with relationship to the same EJB object.

    Eric Chow Newbie

      Hello,

      Is it possible to make a relationship to the same EJB object?


      For example, as in a tree structure.



      | --> a1 ---> aa1 --> aa2
      ROOT -
      |--> b1 ---> bb1


      As seen as above, ROOT is a TreeNode, a1, aa1, aa2, b1, bb1 are also TreeNode(s).

      In fact, a TreeNode has many other TreeNode(s).
      A TreeNode belongs only one parent TreeNode.

      Is it possible to define the relationship as above ?

      Best regards,
      Eric







        • 1. Re: CMR with relationship to the same EJB object.
          David Ward Newbie

          Yes; I do it. Don't think of it as parent->children. Think of it as child->parent, unidirectional. That way, it's easy to look upward to get the parent. Now, to get the children, using an ejb-ql finder that finds all children with a parent of the id.

          For example:

          public abstract class TreeNodeBean implements EntityBean {
          public abstract void setParent(TreeNodeLocal t);
          public abstract TreeNodeLocal getParent();
          }

          public interface TreeNodeLocaleHome extends EJBLocalHome {
          public Collection findChildrenOfParent(Integer parentKey);
          }


          <query-method>
          <method-name>findChildrenOfParent</method-name>
          <method-params>
          <method-param>java.lang.Integer</method-param>
          </method-params>
          </query-method>
          <ebj-ql>SELECT OBJECT(t) FROM TreeNode t WHERE t.parent.key = ?1</ejb-ql>