Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

java - Why merging is not cascaded on a one to many relationship

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

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.

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

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

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.

What could be the cause?

UPDATE

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.

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...