Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6548947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:02:00+00:00 2026-05-25T12:02:00+00:00

I have a ShoppingCart which has a set of CartItem objects .When I save

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:02:00+00:00Added an answer on May 25, 2026 at 12:02 pm

    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 CartItems 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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 lists. A shopping cart list, which contains a objects with the
I have a shopping cart which has SEO links, i'm creating a option to
I have the following form (below) which has a hidden input tag. On form
I have a MySQL table which contains a list of all possible products a
I have a users_manager engine which has a User model class. In an other
I have a shopping cart array, which has a variable to tell me if
Hi i'm creating an app which have shopping cart and payment done via paypal
I'm working on a shopping cart in a website and I have my items(which
Alright, Heres the thing. I have a (paid for) shopping cart, which I am
I have a rails app (rails 3.1.3) that has a shopping cart model. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.