Return-Path: Delivered-To: apmail-openjpa-users-archive@locus.apache.org Received: (qmail 49201 invoked from network); 7 Apr 2008 23:09:39 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 7 Apr 2008 23:09:39 -0000 Received: (qmail 44800 invoked by uid 500); 7 Apr 2008 23:09:39 -0000 Delivered-To: apmail-openjpa-users-archive@openjpa.apache.org Received: (qmail 44781 invoked by uid 500); 7 Apr 2008 23:09:39 -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 44771 invoked by uid 99); 7 Apr 2008 23:09:38 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 07 Apr 2008 16:09:38 -0700 X-ASF-Spam-Status: No, hits=1.2 required=10.0 tests=SPF_HELO_PASS,SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (athena.apache.org: local policy) Received: from [212.227.126.188] (HELO moutng.kundenserver.de) (212.227.126.188) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 07 Apr 2008 23:08:56 +0000 Received: from [192.168.0.234] (host86-129-117-158.range86-129.btcentralplus.com [86.129.117.158]) by mrelayeu.kundenserver.de (node=mrelayeu8) with ESMTP (Nemesis) id 0ML31I-1Jj0SX2PDf-0003YO; Tue, 08 Apr 2008 01:09:05 +0200 Message-ID: <47FAA990.6040804@cyberspaceroad.org> Date: Tue, 08 Apr 2008 00:09:04 +0100 From: Adam Hardy User-Agent: Icedove 1.5.0.14pre (X11/20080305) MIME-Version: 1.0 To: users@openjpa.apache.org Subject: Re: persisting an entity and JPA behaviour with referenced entities References: <47FA0EDE.10902@cyberspaceroad.org> <72c1350f0804070906v68c5f219h61e4372ccb014042@mail.gmail.com> In-Reply-To: <72c1350f0804070906v68c5f219h61e4372ccb014042@mail.gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Provags-ID: V01U2FsdGVkX19tySevIfCV7KQr4LZ+YMKHHy3r9rPgV4UiiXf 1x1fwFDSsiBk9/kDxFYF8V0Jdagj/Y8/3pyRvynl0iXdVv9CRV 4W1uHf+Ku8Ht8diJqwVh+d7KSvBf7QU X-Virus-Checked: Checked by ClamAV on apache.org So theoretically, a merge will also persist? Actually I started out this project assuming it would, but because it has to work with TopLink as well OpenJPA, I stopped using merge() when I came across a bug in the TopLink implementation. But TopLink aside, thanks for the reminder. Michael Dick on 07/04/08 17:06, wrote: > It looks like the persist is being cascaded to the detached entity. If > that's the case then we're throwing the exception per these bullets in the > JPA spec : > > 3.2.1 Persisting an Entity Instance > A new entity instance becomes both managed and persistent by invoking the > persist method on it or > by cascading the persist operation. > The semantics of the persist operation, applied to an entity X are as > follows: > > • If X is a detached object, the EntityExistsException may be thrown > when the persist > operation is invoked, or the EntityExistsException or another > PersistenceException > may be thrown at flush or commit time. > • For all entities Y referenced by a relationship from X, if the > relationship to Y has been annotated > with the cascade element value cascade=PERSIST or cascade=ALL, the persist > operation is applied to Y. > > If you were to merge the new entity instead of persisting it then the merge > action would be cascaded to the parent entity. It would become managed, but > that might be one way to resolve the issue you're hitting. > > -Mike > > On Mon, Apr 7, 2008 at 7:09 AM, Adam Hardy > wrote: > >> I've got an issue with the persist operation, when I use a detached entity >> as one of the entity's referenced entities. >> >> OpenJPA throws the >> org.apache.openjpa.persistence.EntityExistsException: Attempt to persist >> detached object >> "org.apache.openjpa.enhance.org$permacode$atomictest$domain$Genus$pcsubclass@1c527be >> ". >> >> The situation is this: my MVC layer has received a new entity which it >> must create. The parent entity for this is found in a cache, in a detached >> state. >> >> What I'd like to know, is why is JPA forcing me to merge this detached >> entity before allowing me to persist the new child? >> >> It means I can't use the cache, or I have to program the DAO to merge all >> referenced entities. This latter option seems like a job that JPA should be >> doing. JPA knows this parent is a detached entity, so why can't it merge the >> managed entity? >> >> I can't see any text in the EJB spec that would mandate this behaviour, >> yet Hibernate does it too.