openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ekin Sokmen <ekin.sok...@gmail.com>
Subject Re: Merge detached object graph
Date Mon, 01 Sep 2008 14:24:01 GMT

Hi,

This is similar to my issue I posted earlier:
http://n2.nabble.com/Error-while-merging-object-tree-tp364243p364243.html .
The only workaround was adding cascade.persist and cascade.merge to your
backlink. But I'm not sure if this is the proper way to solve this issue.
After a while using this workaround I'm now faced with a strange "Duplicate
entry '0' for key" error where no ID (0) is generated for C and
unfortunately this error does not occur always :(

Any ideas?

Thanks.

ekin.


gmo wrote:
> 
> 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-tp794195p797896.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Mime
View raw message