Hi gang,
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 Store
s, 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);