persistence@glassfish.java.net

Re: Remove persistent object from collection: Best practice

From: Greg Ederer <greg_at_ergonosis.com>
Date: Wed, 08 Nov 2006 12:05:15 -0800
Marina Vatkina wrote:
Greg Ederer wrote:
Marina Vatkina wrote:

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)?


I meant to add the relationship on the other side ;)

Sorry, I misunderstood.

Since I do not have a unique Offering instance per ShoppingCartItem,

You do or you don't?

I do not.
Offering cannot have a ShoppingCartItem property.  Currently, ShoppingCartItem has:

  @OneToOne(optional=false)
  private Offering offering;

And I have nothing specified at the Offering end.  Is there a better way to model this?

If it's OneToOne, can you add a ShoppingCartItem property to the Offering?
What would be wrong about it? If you do, you just do
   getItems().remove(offering.getItem())
   offering.setItem(null);

Is it possible to have a OneToOne relationship that is backed by a join table so that I would not have to have a separate row in my offering table for each Item that has a reference to a particular Offering?  I'm looking at Oracle's JPA Annotation Reference, and I don't see a way to do this.  Can a bidirectional OneToOne have an associated JoinTable?

(I hope I'm asking the right question here.)

Thanks!

Greg

thanks,
-marina


Thanks!

Greg


-marina


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 <http://openoffice.org/>
|




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