Tutorial :JPA @ManyToMany Writing On Create But Not Update



Question:

My team has two classes, User and Store, related by JPA @ManyToMany annotations. The relevant code is below.

When creating a new User object and setting its stores, life is good. But we're seeing some unexpected behavior when trying to update User objects through our Struts 2 / Spring web UI. (These problems do not occur when running simple automated integration tests through JUnit).

Simply put, when saving a User, its stores collection is not updated -- the database shows no changes, and reloads of the User object in question shows it to have an unchanged set of stores.

The only solution that we have found to this problem -- and we don't like this solution, it makes all of the developers on our team a little queasy -- is to create a new Set of Stores, then do user.setStores(null), then do user.setStores(stores).

We are using the OpenEntityManagerInViewFilter; we are using Hibernate as our JPA provider; we are using Spring's JpaTransactionManager. We don't have any @Transactional annotations in our code -- and adding them breaks the existing code due to proxy behavior described elsewhere.

Any insight anyone might provide as to how we might solve this problem in a non-queasy manner is welcome.


Relevant part of User.java:

@ManyToMany  @JoinTable(name = "company.user_store_access",          joinColumns = @JoinColumn(name = "userid"),          inverseJoinColumns = @JoinColumn(name = "storeid"))  public Set<Store> getStores() {      return stores;  }  

Relevant part of Store.java:

  @ManyToMany(mappedBy = "stores")      public List<User> getUsers() {          return users;      }  

Relevant parts of UserDetailAction.java: (pass-throughs down a layer or two, and then:)

entity = getEntityManager().merge(entity);  


Solution:1

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.


Solution:2

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.


Solution:3

Please try the following annotation in your User object:

@ManyToMany(cascade = {CascadeType.ALL})  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »