Return-Path: Delivered-To: apmail-openjpa-users-archive@minotaur.apache.org Received: (qmail 38593 invoked from network); 16 Jul 2009 16:38:48 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 16 Jul 2009 16:38:48 -0000 Received: (qmail 67595 invoked by uid 500); 16 Jul 2009 16:39:53 -0000 Delivered-To: apmail-openjpa-users-archive@openjpa.apache.org Received: (qmail 67524 invoked by uid 500); 16 Jul 2009 16:39:53 -0000 Mailing-List: contact users-help@openjpa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@openjpa.apache.org Delivered-To: mailing list users@openjpa.apache.org Received: (qmail 67514 invoked by uid 99); 16 Jul 2009 16:39:53 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 16 Jul 2009 16:39:53 +0000 X-ASF-Spam-Status: No, hits=-0.6 required=10.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_MED,SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (nike.apache.org: local policy) Received: from [64.18.2.28] (HELO exprod7og125.obsmtp.com) (64.18.2.28) by apache.org (qpsmtpd/0.29) with SMTP; Thu, 16 Jul 2009 16:39:41 +0000 Received: from source ([74.125.92.146]) by exprod7ob125.postini.com ([64.18.6.12]) with SMTP ID DSNKSl9Xt85MaexZtb8PFvi1baKHqhMS0WvM@postini.com; Thu, 16 Jul 2009 09:39:21 PDT Received: by qw-out-1920.google.com with SMTP id 4so87598qwk.10 for ; Thu, 16 Jul 2009 09:39:19 -0700 (PDT) MIME-Version: 1.0 Received: by 10.231.14.12 with SMTP id e12mr2502521iba.48.1247762359205; Thu, 16 Jul 2009 09:39:19 -0700 (PDT) Date: Thu, 16 Jul 2009 12:39:19 -0400 Message-ID: Subject: Deleting children as part of parent merge From: Daryl Stultz To: OpenJPA User List Content-Type: multipart/alternative; boundary=0003255732ee2f4040046ed5501d X-Virus-Checked: Checked by ClamAV on apache.org --0003255732ee2f4040046ed5501d Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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 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. 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 --0003255732ee2f4040046ed5501d--