JPA @ManyToMany在Create上写但不更新 [英] JPA @ManyToMany Writing On Create But Not Update

查看:142
本文介绍了JPA @ManyToMany在Create上写但不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队有两个类,分别是UserStore,它们与JPA @ManyToMany批注相关.相关代码如下.

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

当创建一个新的User对象并设置其存储时,寿命会很长.但是,当尝试通过Struts 2/Spring Web UI更新用户对象时,我们看到了一些意外的行为. (通过JUnit运行简单的自动化集成测试时,不会出现这些问题.

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

简而言之,保存User时,其store集合不会更新-数据库未显示任何更改,并且重新加载有问题的User对象显示其存储组未更改.

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.

我们找到的唯一解决方案-我们不喜欢这种解决方案,它使我们团队中的所有开发人员都有些不舒服-创建新的Set Store s,然后执行user.setStores(null),然后执行user.setStores(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).

我们正在使用OpenEntityManagerInViewFilter;我们正在使用Hibernate作为我们的JPA提供程序;我们正在使用Spring的JpaTransactionManager.我们的代码中没有任何@Transactional批注-添加它们会由于其他地方描述的代理行为而破坏现有代码.

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.

User.java的相关部分:

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

Store.java的相关部分:

Relevant part of Store.java:

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

UserDetailAction.java的相关部分: (直通一两层,然后:)

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

entity = getEntityManager().merge(entity);

推荐答案

问题是您具有双向关系,在这种情况下,存在一个控制另一个实体的实体,而从您的映射中,控制一个实体是 Store 实体而不是 User ,因此可以将用户添加到商店中(因为商店是控制者),但是不能将商店添加到用户中

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.

通过将User对象作为控制关系的对象,将@ManyToMany(mappedBy ="users")批注放在getStores()方法上,并将getUsers()更改为一个,来尝试改变这种情况.

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.

希望有帮助.

P.S.控制关系的实体始终是不具有mapledBy属性的实体.

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

这篇关于JPA @ManyToMany在Create上写但不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆