You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Malte <ma...@googlemail.com> on 2017/04/24 15:55:11 UTC

Problem with EntityExistsException

Hello all!

In a special situation, I'll get a wrong EntityExistsException.

I've extract the problem to a simple project (attached).

I do this:
   try {
     entManager.persist(data);
     entManager.flush();
     entManager.detach(data);

     doOtherDBCallsWithError();

     entManager.getTransaction().commit();
   } catch (Exception e) {
      entManager.getTransaction().rollback();

      entManager.getTransaction().begin();
      entManager.persist(data);
      entManager.getTransaction().commit();
   }

If doOtherDBCallsWithError throws an Exception, it is not possible to
persist the object in the catch-block. The second persists throws
"org.apache.openjpa.persistence.EntityExistsException: Attempt to persist
detached object "de.test.model.Test-1".  If this is a new instance, make
sure any version and/or auto-generated primary key fields are null/default
when persisting." If I use merge instead, the object will also not persist.
If I remove the detach, everything works.

Is this a bug or is the call order persist-flush-detach-commit against the
specification?

Best regards,
Malte

Re: Problem with EntityExistsException

Posted by Malte <ma...@googlemail.com>.
Hi!

As I wrote, this example is an excerpt from a bigger part. So it is not
just a single move from try to finally, I have to rewrite some more code.

Is there any workaround possbile to remove the entity information manually?

Best regards,
Malte

2017-04-24 21:13 GMT+02:00 Mark Struberg <st...@yahoo.de.invalid>:

> Hi Malte!
>
> puh, that's a pretty tricky puzzler.
>
> You flush and detach. At this point the 'data' instance has the
> information that it got inserted.
> Since the entity got detached it also has no further information that the
> transaction got rolled back.
> So technically I can understand what happens.
>
> I also do not think this is ruled out by the spec. Otoh it's surely also
> not defined that it should work.
> So my guess is that you are hitting a rather unspecified area of JPA.
>
> What if you just move the detach to a finally block?
> Would that probably solve your problem?
>
> LieGrue,
> strub
>
> > Am 24.04.2017 um 17:55 schrieb Malte <ma...@googlemail.com>:
> >
> > Hello all!
> >
> > In a special situation, I'll get a wrong EntityExistsException.
> >
> > I've extract the problem to a simple project (attached).
> >
> > I do this:
> >    try {
> >      entManager.persist(data);
> >      entManager.flush();
> >      entManager.detach(data);
> >
> >      doOtherDBCallsWithError();
> >
> >      entManager.getTransaction().commit();
> >    } catch (Exception e) {
> >       entManager.getTransaction().rollback();
> >
> >       entManager.getTransaction().begin();
> >       entManager.persist(data);
> >       entManager.getTransaction().commit();
> >    }
> >
> > If doOtherDBCallsWithError throws an Exception, it is not possible to
> persist the object in the catch-block. The second persists throws
> "org.apache.openjpa.persistence.EntityExistsException: Attempt to persist
> detached object "de.test.model.Test-1".  If this is a new instance, make
> sure any version and/or auto-generated primary key fields are null/default
> when persisting." If I use merge instead, the object will also not persist.
> If I remove the detach, everything works.
> >
> > Is this a bug or is the call order persist-flush-detach-commit against
> the specification?
> >
> > Best regards,
> > Malte
> > <test.zip>
>
>

Re: Problem with EntityExistsException

Posted by Mark Struberg <st...@yahoo.de.INVALID>.
Hi Malte!

puh, that's a pretty tricky puzzler.

You flush and detach. At this point the 'data' instance has the information that it got inserted. 
Since the entity got detached it also has no further information that the transaction got rolled back.
So technically I can understand what happens.

I also do not think this is ruled out by the spec. Otoh it's surely also not defined that it should work.
So my guess is that you are hitting a rather unspecified area of JPA.

What if you just move the detach to a finally block?
Would that probably solve your problem?

LieGrue,
strub
 
> Am 24.04.2017 um 17:55 schrieb Malte <ma...@googlemail.com>:
> 
> Hello all!
> 
> In a special situation, I'll get a wrong EntityExistsException.
> 
> I've extract the problem to a simple project (attached).
> 
> I do this:
>    try {
>      entManager.persist(data);
>      entManager.flush();
>      entManager.detach(data);
>             
>      doOtherDBCallsWithError();            
>             
>      entManager.getTransaction().commit();
>    } catch (Exception e) {
>       entManager.getTransaction().rollback();
>             
>       entManager.getTransaction().begin();
>       entManager.persist(data);
>       entManager.getTransaction().commit();
>    }     
> 
> If doOtherDBCallsWithError throws an Exception, it is not possible to persist the object in the catch-block. The second persists throws "org.apache.openjpa.persistence.EntityExistsException: Attempt to persist detached object "de.test.model.Test-1".  If this is a new instance, make sure any version and/or auto-generated primary key fields are null/default when persisting." If I use merge instead, the object will also not persist. If I remove the detach, everything works.
> 
> Is this a bug or is the call order persist-flush-detach-commit against the specification?
> 
> Best regards,
> Malte
> <test.zip>