persistence@glassfish.java.net

Re: Remove persistent object from collection: Best practice

From: Greg Ederer <greg_at_ergonosis.com>
Date: Wed, 08 Nov 2006 11:08:25 -0800
Hi Marina,

In my model, instances of Offering represent things that a Vendor offers for sale.  Product and Service directly subclass Offering and are, themselves, subclassed by classes representing more specialized kinds of products and services (e.g., Subscription, Garment, et cetera).

A ShoppingCartItem has an Offering (as well as a quantity, a unit price, a collection of Discounts, et cetera).

I suppose that I could establish a many-to-many relationship between Offering and ShoppingCartItem.  Does that strike you as being a good idea?

Cheers,

Greg

Marina Vatkina wrote:
Hi Greg,

Does it mean that there is no relationship from Offering to Item?
Is it worth adding (and maintaining so that they are always at sync)?

thanks,
-marina

Greg Ederer wrote:
Hi,

I have a method that removes a persistent object from a collection based on a property of the object being removed, like so:

  /**
   * Removes all ShoppingCartItems containing this
   * offering
   */
  public void removeItem(Offering offering)
  {
    List<ShoppingCartItem> iList = new ArrayList<ShoppingCartItem>(getItems());
    for(ShoppingCartItem i : iList)
    {
      if(i.getOffering().equals(offering))
      {
        getItems().remove(i);
      }
    }
  }

Is this the best way to do this?

Cheers,

Greg

p.s.: I'm so glad I found this list.  I feel like I've been groping in the dark with JPA.  Thanks for all the help! --G
-- 

| E R G O N O S I S
| Greg Ederer
| Lead Developer
| greg@ergonosis.com
| 360.379.1157
| | OpenDocument - OK <http://openoffice.org/>
|




--
| E R G O N O S I S
| Greg Ederer
| Lead Developer
| greg@ergonosis.com
| 360.379.1157
| 
| OpenDocument - OK
|