You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Daryl Stultz <da...@6degrees.com> on 2009/07/16 18:39:19 UTC
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
Re: Deleting children as part of parent merge
Posted by Daryl Stultz <da...@6degrees.com>.
On Thu, Jul 16, 2009 at 2:30 PM, Ravi Palacherla <ravi.palacherla@oracle.com
> wrote:
> Hi Daryl Stultz,
>
> Can you please give details on what c.delete() does ?
Sorry, I glossed over that when I was sanitizing the sample:
em.remove(em.merge(this)); // where "this" is "c"
--
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com
RE: Deleting children as part of parent merge
Posted by Ravi Palacherla <ra...@oracle.com>.
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