Hitrene
Hitrene

Reputation: 381

How best to store user data in Spring?

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

Upvotes: 0

Views: 2167

Answers (1)

chikincrow
chikincrow

Reputation: 393

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.

Upvotes: 1

Related Questions