openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ravi Palacherla <ravi.palache...@oracle.com>
Subject RE: Deleting children as part of parent merge
Date Thu, 16 Jul 2009 18:30:23 GMT
Hi Daryl Stultz,

Can you please give details on what c.delete() does ?

Regards,
Ravi.

-----Original Message-----
From: Daryl Stultz [mailto:daryl@6degrees.com] 
Sent: Thursday, July 16, 2009 10:39 AM
To: OpenJPA User List
Subject: Deleting children as part of parent merge

Hello,
Suppose I have entity Parent and entity Child. Parent has a collection of
children with CascadeType.ALL. As part of my "save" routine, I want to
delete a Child if it has a "value" field of zero. This is what I am doing
now:

Iterator<Child> iter = parent.getChildren().iterator();
while (iter.hasNext()) {
Child c = iter.next();
if (c.getValue() == 0.0) {
iter.remove();
if (c.getId() != null) c.delete();
}
}
em.merge(parent);

Notice I am removing the deleted instances from the iterator/backing
collection. This works - at least most of the time. I just got this
exception:

Operation attempted on a deleted instance.
<openjpa-1.2.1-r752877:753278 nonfatal user error>
org.apache.openjpa.persistence.InvalidStateException:
Operation attempted on a deleted instance.
FailedObject: com.company.Child-2606
       at org.apache.openjpa.kernel.PCState.error(PCState.java:443)
       at
org.apache.openjpa.kernel.PDeletedState.beforeOptimisticWrite(PDeletedState.java:76)
       at
org.apache.openjpa.kernel.StateManagerImpl.dirty(StateManagerImpl.java:1575)
       at
org.apache.openjpa.kernel.StateManagerImpl.settingDoubleField(StateManagerImpl.java:1797)
       at
org.apache.openjpa.kernel.DetachedStateManager.attach(DetachedStateManager.java:218)
       at
org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:241)
       at
org.apache.openjpa.kernel.AttachStrategy.attachCollection(AttachStrategy.java:333)
       at
org.apache.openjpa.kernel.DetachedStateManager.attach(DetachedStateManager.java:255)
       at
org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:241)
       at
org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:101)
       at org.apache.openjpa.kernel.BrokerImpl.attach(BrokerImpl.java:3212)
       at
org.apache.openjpa.kernel.DelegatingBroker.attach(DelegatingBroker.java:1158)
       at
org.apache.openjpa.persistence.EntityManagerImpl.merge(EntityManagerImpl.java:769)

I don't know why it would work most of the time or exactly why it failed
this time, but I'm wondering what's the best way to implement this effect?
Is it not proper to remove the child from the collection?

Thanks.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Mime
View raw message