为什么合并没有在一对多关系上级联 [英] Why merging is not cascaded on a one to many relationship

查看:141
本文介绍了为什么合并没有在一对多关系上级联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里的问题与其他问题几乎相同显式删除JPA关系 但是我想进一步简化它以得到更详细的答案.

My question here is almost similar to my other question Explicit delete on JPA relationships but I thought of simplifying it further to warrant more detailed answers.

想象一下,我在父母和孩子之间有一个一对多的关系.

Imagine I have a OneToMany relationship between a parent and a child.

@Entity
public class Parent {
    private String name;
    @OneToMany(mappedBy="owner",cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    private List<Child> children;
}
@Entity
public class Child {
    private String name;
    @ManyToOne
    private Parent owner;
}

在我的用户界面中,我正在显示父母名单.用户可以选择一个父母,他/她可以编辑其子女的列表. 为了说明这一点,我使用了currentParent变量来表示当前已编辑的父级. 用户可以选择添加/编辑孩子的名字.

In my UI, I am showing a list of parent. User can choose one parent and he/she can edit the list of its children. To show this, I have used a currentParent variable to represent the current parent edited. User can choose to add/edit children's name.

单击按钮后,我将使用EJB调用保存新的子级列表.

Upon clicking a button, I would save the new children's list using an EJB call.

@ViewScoped
public class MyBean(){
    private Parent currentParent;
    @EJB
    private MyEJB myEJB;

    public void saveChanges(){
        myEJB.save(currentParent);
    }
}

我的EJB调用看起来像这样.

My EJB call looks like this.

@Stateless
public class MyEJB{
    @PersistenceContext(unitName = "MyPU")
    private EntityManager em;

    public void save(Parent parentNew){
        Parent pOld = em.find(entityClass,  p.getId());
        if(pOld !=null){
            pOld.setName(parentNew.getName());
            pOld.setChildren(parentNew.getChildren());
            em.merge(pOld);
        }
    }
}

我的问题是,我能够更改父级的名称,但是对子级列表的任何更改都不会反映到数据库中. 它不会更改或执行任何SQL来删除/更新/添加子项.

My problem is that, I am able to change the name of the parent but any changes to the childrens list is not reflected into the DB. It does not alter or execute any SQL to delete/update/add childs.

可能是什么原因?

更新

经过一些反复试验并阅读了各种链接之后,在我的@onetomany中设置orphanRemoval选项似乎可以解决我的问题. Eclipselink在合并过程中会自动删除孤立行.

Well after some trial and error and reading thru various links, it seems that setting the orphanRemoval option in my @onetomany would solve my problem. Eclipselink automatically deletes the orphan rows during merging.

JPA的复杂性如此之多.

So much for JPA complexity..

@Entity
public class Parent {
    private String name;
    @OneToMany(mappedBy="owner",cascade=CascadeType.ALL, fetch=FetchType.EAGER,orphanRemoval = true)
    private List<Child> children;
}

推荐答案

这是合并的标准问题.对集合中子对象的更改将被提取.

This is a standard problem with merge. Changes to child objects in the collection will get picked up.

merge调用只能级联列表中的对象,因此对不在列表中的对象的更改会丢失.不幸的是,oneToMany不拥有这种关系:孩子的ManyToOne拥有.因此,需要合并子实体,以便可以看到更改并将其推送到数据库.子实体是独立的对象,并且在更改时需要单独合并.或添加到其他父树/树中以进行合并.

The merge call can only cascade over objects that are in the list, so changes to objects not in the list get lost. Unfortunately, the oneToMany doesn't own the relationship: the child's ManyToOne does. So the child entities need to be merged for changes to be seen and pushed to the database. Child entities are independent objects and need to be merged individually when the are changed. That, or added to a different parent/tree to get merged.

这篇关于为什么合并没有在一对多关系上级联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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