Now I'm trying to develop Internet shop on Spring and now trying to create cart. What is the best way to store data like count, sum and list of products in the cart? I used HttpSession and Cookies, but Cookies can't store lists...
1 Answers
The cart is a pojo, those fields are properties of your object. You can persist it in the database, or in session.
To do it in session, example:
There is no best way, just the way you should do it given the context you have.
But persisting it in database allows you to have a cart history, you can remember the user cart after the session is gone, and you scale more easily because you can share your state between several server instances.
To do it with entities:
Just a proposition:
You can associate a anonymous account to the user session, and this anon account owns a cart. If the user create a real account, you transform the anon account to real account and keep the cart to order. If the user never go back, you should have a rule to clean the anon data every once in a while. If you can know the user email while it is an anon user (example: popup to register to newsletter), you can call him back to remember him that he has a waiting cart on the website.

- 1
- 1

- 393
- 3
- 11
-
Thanks. How I will recognise the user if I'll use database? I should keep user ip like a key paramater? – Hitrene Jul 22 '16 at 21:38
-
Hello. You can associate an anonymous user account to a the user session. The anon user account owns the cart, if the anonymous user loose its session, you clean everything every x days (you might want to keep the anon carts some time depending on your business analysis). If the user create an account, you can promote his anon account to real user, and he keep its data. Maybe is too complicated, maybe it is what you want or maybe not. It is just a proposition. I edit the answer. – chikincrow Jul 22 '16 at 21:55
-
Thanks again. It's very helpful – Hitrene Jul 22 '16 at 22:25