【问题标题】:How to Cascade Update in JPA如何在 JPA 中级联更新
【发布时间】:2011-07-08 14:10:09
【问题描述】:

我在尝试使 ManyToMany 级联事件正常工作时遇到了困难。我浏览过类似的帖子,但与其他帖子相比,我看不出我的代码哪里出错了。

我有两个表Organisation和Project,Organization是关系的所有者,两者之间有一个关系表。

我正在尝试保留一个新的项目实体,该实体可以有一个或多个与之关联的组织。我使用 selectManyListbox(JSF 2) 让用户选择多个组织。在我尝试级联关系之前,这一切都很好。要么只有项目被持久化,要么持久化经理为与其关联的每个组织创建同一项目的新副本。 IE。不同的orgID和projID(而不是不同的orgID指的是同一个projID)

当我尝试保持关系时,为什么 JPA 会创建同一个项目的新实例(相同的属性,除了 ID)?

我正在努力学习 JPA/JSF,非常欢迎任何帮助。问候克里斯。

PS。在 Project bean 控制器中,我得到了用户选择的正确组织实例。

具有主键的实体对象

public class Organisation implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idOrganisation")
private Integer idOrganisation;

..

@JoinTable(name = "organisationproject", joinColumns = {
        @JoinColumn(name = "fkOrganisation", referencedColumnName = "idOrganisation")}, inverseJoinColumns = {
        @JoinColumn(name = "fkProject", referencedColumnName = "idProject")})
@ManyToMany (fetch=FetchType.EAGER, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
private Collection<Project> projectCollection;

..
}

public class Project implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idProject")
private Integer idProject;

..

@ManyToMany(mappedBy = "projectCollection")
    private Collection<Organisation> organisationCollection;
..
}

项目 bean 控制器

@ManagedBean (name="projectController")
@SessionScoped
public class ProjectController implements Serializable {

private Project current;
private UISelectMany selectedOrganisations;

...

public String create() {
        UserController userBean = JsfUtil.findBean("userController", UserController.class);
        current.setUser(userBean.getSessionUser());
        current.setTimeStamp(JsfUtil.getTimeStamp());
        try {


            //Use the OrganisationFacade, who is the owner of the relationship and uses Cascade to update the relation
            for(Organisation org : current.getOrganisationCollection()) {
                org.getProjectCollection().add(current);
                ejbOrganisationFacade.edit(org);

            }

            //getFacade().create(current);

            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/resources/Bundle").getString("ProjectCreated"));
            return prepareCreate();
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/resources/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

public UISelectMany getSelectedOrganisations() {
        return selectedOrganisations;
    }

public void setSelectedOrganisations(UISelectMany selectedOrganisations) {
        this.selectedOrganisations = selectedOrganisations;
    }
...
}

终于有了Organiasation外观编辑功能

@Override
    public void edit(Organisation entity) {
        getEntityManager().merge(entity);
    }

【问题讨论】:

  • 您是否也将 Oranisations 添加到当前项目中?我没有在你的代码中看到它,我认为你需要这样做。
  • 感谢重播。 current.getOrganisationCollection() (current = project) 已经拥有带有 Organisations 的集合。我认为这是通过 JSF 页面中的绑定完成的,binding="#{projectController.selectedOrganisations}".
  • 那么,如果我在组织和项目方面都设置了集合,那可能是什么问题?我会附上jsf页面。
  • @Chris - 请编辑您的问题以提供更多信息。答案是为您的问题的解决方案保留的。
  • @Chris 你能发布你的 bean 的主键是什么吗?

标签: java jsf jpa glassfish ejb-3.0


【解决方案1】:

解决了这个问题,并将重播发布给其他可能与我犯相同错误的人。希望没问题。

问题出在 Project bean 的 create 函数中。在合并(更新)组织实体之前,我必须先在项目上调用 create(persist)。

try {

            getFacade().create(current);

            for(Organisation org : current.getOrganisationCollection()) {
                org.getProjectCollection().add(current);
                ejbOrganisationFacade.edit(org);
            }

            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/resources/Bundle").getString("ProjectCreated"));
            return prepareCreate();
        } catch (Exception e) {

我在项目外观的 create 方法中也有一个错误,它调用了合并而不是持久化。这导致了很多问题。很抱歉为这样一个微不足道的解决方案占用了空间。

【讨论】:

    猜你喜欢
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 2017-01-17
    相关资源
    最近更新 更多