Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA Merge and Remove are not cascaded to child object at the same time
    primarykey
    data
    text
    <p>I'm using Open JPA in the commercial project and would like to take advantage of the cascading Parent->Child remove and merge. </p> <p>I mocked up a working code which shows the issues. </p> <p>I have a persistent Parent object with some children. I'm eliminating one of the Children's relation and passing detached Parent to merge. When transaction is committed a UPDATE statement issued, trying to update orphaned child with NULL foreign key.</p> <pre><code>@Entity public class Parent implements Serializable { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer id; private String desc; //@ElementDependent @OneToMany(mappedBy="parent", cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}/*,orphanRemoval=true, fetch=FetchType.EAGER*/) private List&lt;Child&gt; childs = new ArrayList&lt;Child&gt;(); @Entity public class Child implements Serializable { @Id private String desc; @ManyToOne private Parent parent; public class StackOverflowTest extends TestCase { private EntityManager em; private static EntityManagerFactory factory = Persistence.createEntityManagerFactory("SSICTest", System.getProperties()); private Parent p; private Child c; private Child c2; public void testRemove() { prepareObjects(); startTr(); em.persist(p); commitTr(); startTr(); p = em.find(Parent.class, p.getId()); em.remove(p); commitTr(); } public void testMerge() { prepareObjects(); startTr(); em.persist(p); commitTr(); //remove on detached Child child = p.getChilds().get(0); p.getChilds().remove(child); child.setParent(null); startTr(); em.merge(p); commitTr(); startTr(); p = em.find(Parent.class, p.getId()); assertEquals(1, p.getChilds().size()); commitTr(); } protected void prepareObjects() { p = new Parent(); c = new Child(); c2 = new Child(); p.setDesc("desc"); c.setDesc(Math.random()+""); c2.setDesc(Math.random()+""); p.getChilds().add(c); c.setParent(p); p.getChilds().add(c2); c2.setParent(p); } void commitTr() { em.getTransaction().commit(); em.close(); } void startTr() { em = factory.createEntityManager(); em.getTransaction().begin(); } } </code></pre> <p>In the example above testRemove works fine but testMerge method not, as I described at the top. </p> <p>If I remove comment on @ElementDependent it works different. </p> <p>testRemove fails, because remove not cascaded to Child and referential integrity exception thrown by db, and testMerge is fine. </p> <p>orphanRemoval=true, fetch=FetchType.EAGER or @ForeignKey(deleteAction=ForeignKeyAction.CASCADE) on inverse relation in child do not help too. </p> <p>Please advise. I really appreciate your help!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload