I have three Entities:
A, B and C

A has a @OneToMany(cascade=CascadeType.ALL) relation to B and
B has a @OneToMany(cascade=CascadeType.ALL) relation to C

Consequently,
C has a @ManyToOne relation to B and
B has a @ManyToOne relation to A


Now consider the following code:


EntityManager em;

em = createEntityManager();
em.getTransaction().begin();
A a = em.findByKey(A.class, 123); //load A
em.getTransaction().commit();
em.close(); //detach

//graph is: a ---(1:n)---> b ---(1:n)---> c
B b = new B();
C c = new C();

//forwardlinks
a.getChilds().add(b);
b.getChilds().add(c);

//backlinks
c.setParent(b);
b.setParent(a);

em = createEntityManager();
em.getTransaction().begin();
em.merge(a);
em.getTransaction().commit();
em.close();


OpenJPA now reports the following error upon flush:
Encountered unmanaged object in persistent field "C.parent" during flush. 
However, this field does not allow cascade persist. Set the cascade
attribute for this field to CascadeType.PERSIST or CascadeType.ALL (JPA
annotations) or "persist" or "all" (JPA orm.xml), or enable cascade-persist
globally, or manually persist the related field value prior to flushing. You
cannot flush unmanaged objects or graphs that have persistent associations
to unmanaged objects.


When removing the backlinks (see above), everything works fine.


My assumption is, because the Entities were detached, OpenJPA lays out the
graph as:
a --> b --> c --> b


Think this has to do with:
"public Object merge(Object entity);
If A is a detached entity, its state is copied into existing managed
instance A' of the same entity identity, or a new managed copy of A is
created."

-- 
View this message in context: 
http://n2.nabble.com/Merge-detached-object-graph-tp794195p794195.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to