what to do with cartitems in db when a shoppingcart is cleared
I have a ShoppingCart
which has a set of CartItem
objects .When I save the shoppingcart,all the cartitems are also saved.When the buyer confirms a buy,I need to clear the shoppingcart.If I save the cart to the db now,what should happen to the cartitems already saved in the db and associated with the cart?Should I remove them from db?
The jpa mappings of Entities are
@Entity
class ShoppingCart{
...
@OneToOne
public Buyer buyer;
@OneToMany(mappedBy="cart", cascade=CascadeType.ALL)
public Set<CartItem> cartItems;
...
}
@Entity
public class CartItem{
@ManyToOne
public ShoppingCart cart;
@OneToOne
public Product pdt;
public int quantity;
...
}
}
the shoppingcart table in db
id | buyer_id
-----+-------------
100 | 50
cartitem ta开发者_JAVA百科ble can be
id | quantity | product_id | cart_id
-----+----------+------------+---------
12 | 2 | 234 | 100
--------------------------------------
13 | 4 | 231 | 100
So after I clear the shoppingcart and save it to db,if these items are still in db,it would mean that cartitem 12 still refers to cart 100.But ,cart with id=100 has no cartitems since I have cleared them. So,is clearing a cart and saving it to db equivalent to deleting the cart? How do I map this behaviour?Or is there a flaw in my thinking?
You haven't posted code on how you are clearing the shopping cart, so my answer will make a fair number of assumptions.
Going by the contents of the tables, I'll assume that you've invoked cartItems.clear()
in your ShoppingCart
class. The problem with this invocation is that your relationship is bidirectional and therefore a CartItem
instance will continue to have a reference back to the ShoppingCart
instance, although the opposite isn't true. Depending on what JPA provider you are using, clearing the Set
and updating the persistence context contents with the database will either not clear the cartitem
table or will throw an exception stating that the cart_id
cannot be null (if your foreign keys are not nullable).
The fix in most JPA providers (especially in Hibernate) is to clear the reference to the ShoppingCart
in the CartItem
instance in addition to the cartItems
Set in ShoppingCart
. Note that, if you choose to deleted orphaned entries using the orphanRemoval
attribute of the @OneToMany
annotation (supported since JPA 2.0), then all orphaned CartItems
(that are not referenced by a ShoppingCart
) will also be deleted in the database, on clearing the bidirectional relationship. Without the orphanRemoval
attribute set to true, your JPA provider will make no attempt to delete CartItem
s that are no longer referenced by a ShoppingCart
; the transaction will eventually be successful depending on whether your foreign key in the cartitem table is nullable or not.
If you intend to retain the CartItem
records in the database, but merely nullify the reference to the ShoppingCart
, then you ought to designate the reference as nullable (using the @Column
annotation, and also have the table defined to have a nullable foreign key).
You can either use orphanRemoval (JPA 2), or call em.remove() for each item.
See, http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Orphan_Removal_.28JPA_2.0.29
Otherwise, the items will still be there.
If you want the items to remain in the database, then just set their cart to null to remove them from the cart.
精彩评论