7 Replies Latest reply on Jul 9, 2003 10:58 AM by kgodbole

    3.2.1, Iterator.remove() on Collection

    dcrot

      Calling an ejbSelect method I get a collection of references to local entity beans. I then iterate through the collection calling remove(), as in:

      Collection c = ejbSelectOrders();
      Iterator i = c.iterator();
      while(i.hasNext()) {
      i.next();
      i.remove();
      }

      This however does not delete the order records from the db (no exceptions are thrown), in fact the orders ejbRemove() method is not called. However, if I change the code to be:

      Collection c = ejbSelectAllOrders();
      Iterator i = c.iterator();
      while(i.hasNext()) {
      OrderLocal o = (OrderLocal)i.next();
      o.remove();
      }

      the order records are removed and transaction commited, even though this should throw an IllegalStateException.


      Does anybody have any input into this, I'm using 3.2.1?


      thanks,

      Dean

        • 1. Re: 3.2.1, Iterator.remove() on Collection
          benholland

          I noticed the same behaviour on 3.0.6.

          I got round it by doing

          Set accounts = Customer.getAccounts();

          accounts.remove(localAccount);

          Jboss then removed the link from the account
          to the customer.

          Not sure if you are right about the illegal state exception depends on the type of iterator I think.

          • 2. Re: 3.2.1, Iterator.remove() on Collection
            raja05

            Collection c = ejbSelectOrders();
            Iterator i = c.iterator();
            while(i.hasNext()) {
            i.next();
            i.remove();
            }
            would not call your EJB as the next() returns an Object and not the localejb. Only if you cast it, you get a EJBLocal object..

            • 3. Re: 3.2.1, Iterator.remove() on Collection
              dcrot

              Ben,

              the EJB spec states in 10.3.6.1 that "... the bean provider must not modify the containmer manager collection while the iteration is in progress in any way that causes elements to be added or removed, other than by the Iterator.remove method... or a IllegalStateException should be thrown by the container..."

              Doing it your way means that I need a reference to an entity bean to start with, in my case I'm blindly deleting in all records in the collection.

              thanks,

              Dean

              • 4. Re: 3.2.1, Iterator.remove() on Collection
                benholland

                point taken on the exception front.

                To delete a collection in cmr just set the collection to a zero sized hashset. JBoss will remove the relationships from the RDBMS. If you configure the db or jboss to cascade delete it will remove the rows as well.

                I just tried it it works.

                • 5. Re: 3.2.1, Iterator.remove() on Collection
                  dcrot

                  Ben,

                  I've been treating ejbSelect's as cmr's, which they arent, hence this is the reason why iterator.remove() doesnt work. For example, the Collection implementation for an ejbSelect is java.util.ArrayList, whereas the implemetaiton for a cmr is RelationSet.

                  When you say "set the collection to a zero size hashset", are you talking about the Collection.clear() method?


                  As for cascade-deletes, I cant use them for my many-to-many relationships, and I dont want the db to perform the deletes, as I'd then have to use commit option b or c.


                  thanks,

                  Dean

                  • 6. Re: 3.2.1, Iterator.remove() on Collection
                    benholland

                    Dean,

                    It's not pretty:

                    CustomerLocalHome home = (CustomerLocalHome) ctx.lookup("local/Customer");

                    CustomerLocal cust =
                    home.findByPrimaryKey(new CustomerPK( customerID));

                    cust.setAccounts(new HashSet(0));

                    I dont have experience of ejb select, let me know if it works.

                    • 7. Re: 3.2.1, Iterator.remove() on Collection
                      kgodbole

                      I am using JBoss 3.2.1 and have found that if you try to remove from the collection returned by a cmr-get method, you get IllegaStateException. This is in the specs, as mentioned by Dean.

                      However, if you convert the collection to ArrayList it somehow works.

                      Collection c = ejbSelectAllOrders();
                      ArrayList al = new ArrayList(c);
                      for (Iterator it = al.iterator(); it.hasNext(); ) {
                      ((OrderLocal) (it.next())).remove();
                      }

                      Any explanations will be very welcome.
                      Thanks.