JPA @ManyToMany Writing On Create But Not Update

六眼飞鱼酱① 提交于 2019-12-10 14:28:34

问题


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);

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




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




回答3:


Please try the following annotation in your User object:

@ManyToMany(cascade = {CascadeType.ALL})


来源:https://stackoverflow.com/questions/946938/jpa-manytomany-writing-on-create-but-not-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!