openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Heiko Kopp <>
Subject OpenJPA 1.2: EntityExistsException on em.persist() for Entity that is no longer present in DB
Date Thu, 22 Sep 2011 10:50:37 GMT

I'm using OpenJPA 1.2.2 in our project to handle a database related locking
system for entries. A lock-table is used to determin whether a selected
entry is currently worked with or not. 

I've come to the following situation where I'm unsure what the best way
would be to solve this: 

Person A: Starts working with a selected entry. A lock entry is written to
the database using em.persist(). 
Person B: Tries to work with the selected entry of Person A. The entry is
found in the database, an error is send to Person B that somebody else is
working with this entry. 
Person A: Stops working with the entry. The lock written to the database is
removed from the database. 
Person B: tries again to work with the entry of Person A. 

Now the problem arises: 

em.find() will still find the lock entry in the persistence context although
it is no longer in the database, therefore I'm not able to determine that
actually nobody is working with the entry anymore. 

I added a em.refresh() on the lock entry after the em.find() resulting in an
EntityNotFoundException. I now know, the entry is gone in the database,
although I still have a 'cached' entity. 

As the refresh revealed, that the entity is no longer in the database, I try
to create a new entry (as because of code structure, I do not have access to
the entity stil in the context). When I try to persist this new entity I get
a EntityExistsException, do I try to merge it I get a

What is the correct way to handle this issue? Do I really have to track down
that the entity found by the em is actually cached but not existing in the
database, use this very entity and modify it instead of creating a new one? 

Thank you for clarification. 


View this message in context:
Sent from the OpenJPA Users mailing list archive at

View raw message