JPA @ManyToMany Writing On Create But Not Update JPA @ManyToMany Writing On Create But Not Update spring spring

JPA @ManyToMany Writing On Create But Not Update


The problem is that you have a bi-directional relation, and in this case there is one entity that controls the other, and from your mapping the controlling one is the Store entity and not the User, so adding a user to a store would work (since the Store is the one who controls) but adding a store to a user will not.

try inverting the situation, by making the User object the one who controls the relation, by putting the @ManyToMany(mappedBy = "users") annotation on the getStores() method and changing the getUsers() one.

Hope it helped.

P.S. the entity that controls the relation is always the one that doesn't have the mappedBy attribute.


I just got stuck to same problem and found solution on another page.

Here is what has worked for me

calling getJpaTemplate().flush() before calling merge has worked for me.

I did not get the reason why it worked.


Please try the following annotation in your User object:

@ManyToMany(cascade = {CascadeType.ALL})