openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adam Hardy <>
Subject Re: persisting an entity and JPA behaviour with referenced entities
Date Mon, 07 Apr 2008 23:09:04 GMT
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:
>    <snip>
>     • 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
>> "$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.

View raw message