educative.io

Relationship between cart & user

I don’t understand table relationship between cart & user.

@ManyToOne(targetEntity = User.class, fetch = FetchType.EAGER)
@JoinColumn(nullable = false, name = "user_id")
private User user;

The relationship between User is ManyToOne to Cart entity.
Does It means users can have one same cart?
I think It should be OneToOne relationship because I think It makes sense one user can have only one cart and a cart can belong to only one user at the same time.

1 Like

Hi @Jin

Thank you for reaching out to us. As mentioned in the lesson " Add to Cart Feature", users can have multiple items in the cart. So the relationship between the cart and the user is many-to-one. Because multiple cart items can belong to one user.

We hope Educative has helped you in your learning :slight_smile:

1 Like