Spring boot ManyToMany with counter if exist

Viewed 144

I'm working on a Spring Boot shop and I have a ManyToMany relation between a User table and the Products table. When someone is clicking multiple times on the same product the relation is not created because it exists yet. Is it possible to add a counter variable in the user_product table that give me the possibility to get the amount of same relations? I need to add to the cart the products even when someone clicks on the same product multiple times.

Any possibilities or other solutions? Thanks

1 Answers

For to implement a counter for the Product associated with an User, in the User entity use,

Unidirectional Many-to-Many Relationship

@ManyToMany(target=com.package.Product)
@JoinTable(name="user_products")
Map<Product, Integer> products;

When a product is added for the 1st time, set the value 1, and increment it when it's added for the subsequent times.

JPA 2.2 Specification

Related