users@glassfish.java.net

JPA merge creating new record

From: <glassfish_at_javadesktop.org>
Date: Fri, 06 Jul 2007 04:28:41 PDT

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