users@glassfish.java.net

Re: JPA merge creating new record

From: Sahoo <Sahoo_at_Sun.COM>
Date: Fri, 06 Jul 2007 18:03:15 +0530

What kind of Id (PK) strategy are you using? Is it auto-generated? When
you put the object into the hashtable, is the id set?

-- Sahoo

glassfish_at_javadesktop.org wrote:
> I'm using Glassfish and JPA and have problems updating database records.
>
> The trouble is that whenever I try to update a record by issuing an EntityManager.merge, it seems JPA sees this as a new entity and does a new insert in the database instead of merging my detached entity.
>
> What I'm actually doing is this:
> First, I'm traversing an XML document. This XML has several "records". For each of these records, an object is created and then immediately persisted. These objects are then stored in a hashtable.
>
> A little later in the program, the objects are updated with a status. I do this by looping the hashtable, retrieving each object, updating it and then calling merge on it.
>
> What happens is that JPA creates a new database record instead of merging the entity with the existing database record.
>
> Now, I have read that if the EntityManager does not have track of an entity with the given ID, it will create a new one. So, thinking that this in-and-out-of-hashtable operation might cause the EM to lose track of the entities (for some reason), I've created a simple test program as well, to ensure the EntityManager DOES know about an entity.
>
> This simple program creates an entity, persists it, finds it again (using EntityManager.find), changes it and then merges it. No hashtable or other operations are involved (but several transactions). This reproduces the error. The merge operation creates a new database record instead of updating the existing.
>
> Point me in the right direction and I will (hopefully) soon be a happier man! :-)
>
> Regards
> Marius
> [Message sent by forum member 'mariusw' (mariusw)]
>
> http://forums.java.net/jive/thread.jspa?messageID=225477
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>