You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by "Ram.sankar" <ra...@gmail.com> on 2009/03/09 15:40:47 UTC

Issue in Handling persistent objects across multiple persistence contexts

Hi,

In my application i have a requirement that eventhough i change the values
of persistent objects available in the session directly using getter and
setter methods i have to save only the objects on which the 
EntityManager.persist() is invoked explicitly. To resolve this problem we
maintain 2 managers one for read and another for write. I detach the object
from the read entity manager and merge the same with the write entity
manager before calling a persist(). 
The problem here is that OpenJPA is generating an insert query to save the
persistent object eventhough it exists in the table. This is giving me an
Unique constraint violated error. Is there anyway to tell OpenJPA to check
for the existence of record with same primary key before persisting. Any
help is highly appreciated.

Regards,
Ram
-- 
View this message in context: http://n2.nabble.com/Issue-in-Handling-persistent-objects-across-multiple-persistence-contexts-tp2449327p2449327.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: Issue in Handling persistent objects across multiple persistence contexts

Posted by "Ram.sankar" <ra...@gmail.com>.
Yes rick, i am using merge only, please have a look at the below code:

		writeEm.getTransaction().begin();
		om.detach(beanVar);
		DataObject bean = writeEm.merge(beanVar);
		writeEm.persist(bean);

writeEm = EntityManager for writing alone
om = entityManager for reading alone

Regards,
Ram


Rick Curtis wrote:
> 
> Ram -
> When you want to write your detached Entity back to the DB, are you using
> EntityManager.merge(..)? If not, I'd give that a try.
> 
> -Rick
> 
> Ram.sankar wrote:
>> 
>> Hi,
>> 
>> In my application i have a requirement that eventhough i change the
>> values of persistent objects available in the session directly using
>> getter and setter methods i have to save only the objects on which the 
>> EntityManager.persist() is invoked explicitly. To resolve this problem we
>> maintain 2 managers one for read and another for write. I detach the
>> object from the read entity manager and merge the same with the write
>> entity manager before calling a persist(). 
>> The problem here is that OpenJPA is generating an insert query to save
>> the persistent object eventhough it exists in the table. This is giving
>> me an Unique constraint violated error. Is there anyway to tell OpenJPA
>> to check for the existence of record with same primary key before
>> persisting. Any help is highly appreciated.
>> 
>> Regards,
>> Ram
>> 
> 
> 

-- 
View this message in context: http://n2.nabble.com/Issue-in-Handling-persistent-objects-across-multiple-persistence-contexts-tp2449327p2453321.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: Issue in Handling persistent objects across multiple persistence contexts

Posted by Rick Curtis <cu...@gmail.com>.
Ram -
When you want to write your detached Entity back to the DB, are you using
EntityManager.merge(..)? If not, I'd give that a try.

-Rick

Ram.sankar wrote:
> 
> Hi,
> 
> In my application i have a requirement that eventhough i change the values
> of persistent objects available in the session directly using getter and
> setter methods i have to save only the objects on which the 
> EntityManager.persist() is invoked explicitly. To resolve this problem we
> maintain 2 managers one for read and another for write. I detach the
> object from the read entity manager and merge the same with the write
> entity manager before calling a persist(). 
> The problem here is that OpenJPA is generating an insert query to save the
> persistent object eventhough it exists in the table. This is giving me an
> Unique constraint violated error. Is there anyway to tell OpenJPA to check
> for the existence of record with same primary key before persisting. Any
> help is highly appreciated.
> 
> Regards,
> Ram
> 

-- 
View this message in context: http://n2.nabble.com/Issue-in-Handling-persistent-objects-across-multiple-persistence-contexts-tp2449327p2449773.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: Issue in Handling persistent objects across multiple persistence contexts

Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
  OpenJPA uses a series of decisions to determine whether a given argument x
as in merge(x) is a detached instance of not. The most obvious of this
decisions is based on a version field in the entity. So the first suggestion
will be to add a @Version field, if it is not defined already. 
  If OpenJPA is inserting the merge entity, most likely that it has no
version field and no detached state (another mechanism OpenJPA uses that
adds few extra bits in the detached version of the instance). 

  
-- 
View this message in context: http://n2.nabble.com/Issue-in-Handling-persistent-objects-across-multiple-persistence-contexts-tp2449327p2453664.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.