2 Replies Latest reply on Jul 27, 2002 1:00 PM by Tim Fox

    CMP ejbRemove(): Underlying collection has been modified.

    Sergei Batiuk Newbie

      Hello,

      I have a TreeNode bean that implements a hierarchical structure. It has a CMR field parent, with type of TreeNode. There is also a CMR field children, with type of java.util.Collection. Both of these fields comprise a single one-many relationship.

      My ejbRemove() method is as follows:

      public void ejbRemove() {
      Iterator children = getChildren().iterator();
      while ( children.hasNext() ) ( ( TreeNode )children ).remove();
      }

      However, I get the "Underlying collection has been modified" exception. Is it OK?

      If I add an ejbSelect method called "ejbSelectChildren( TreeNode parent ); with a query "SELECT OBJECT(tn) FROM TreeNodeBean tn where tn.parent=?1", and redefine my ejbRemove() method as

      public void ejbRemove() {
      Iterator children = ejbSelectChildren( ( TreeNode )entityContext.getEJBLocalObject() ).iterator();
      while ( children.hasNext() ) ( ( TreeNode )children ).remove();
      }

      Everything works fine.

      Why can't I just use a collection return with getChildren()? Is it a bug or a feature?

      Thanks in advance.
      Sergei Batiuk.

        • 1. Re: CMP ejbRemove(): Underlying collection has been modified
          Sergei Batiuk Newbie

          There has been a mistake in my previous message:

          my ejbRemove() method, that doesn't work (but should work in my opinion):
          ---------------------------------

          public void ejbRemove() {
          Iterator children = getChildren().iterator();
          while ( children.hasNext() ) ( ( TreeNode )children.next() ).remove();
          }


          ejbRemove() that works, but requires addition ejbSelect() method:
          ---------------------------------
          public void ejbRemove() {
          Iterator children = ejbSelectChildren( ( TreeNode ) entityContext.getEJBLocalObject() ).iterator();
          while ( children.hasNext() ) ( ( TreeNode )children.next() ).remove();
          }

          ---------------------------------
          All this wouldn't be an issue if cascade-delete worked fine (which doesn't work, it seems, in BEA Weblogic 7 properly too).

          • 2. Re: CMP ejbRemove(): Underlying collection has been modified
            Tim Fox Master

            You need to remove the item from the iterator
            ie iter.remove(), just before the call to yourObject.remove().
            you are getting a collectionmodifiedexception since your call yourObject.remove() removes the object from the collection you are currently iterating through thus making it invalid.
            This is a side-effect of what the ejb2 spec mandates for what happens when you remove an entity.
            There is a whole section in the ejb2 spec on the correct semantics for adding and removing entities from cmr relations.