Eclipse – Removing an entity, but using the same primary key to add a similar entity after the removal


Im trying to remove an entity which has a unique PK like : 80253

I remove this entity by doing the follow lines of code:

myEntityType1 = getEntityManager().find(MyEntityType1.class, 80253);

These bits of code actually deletes the rows from my database and all its cascading objects properly, and Im very happy about this. Now the problem occurs when I now need to create a similar entity that uses the same primary key (which should now be gone right?).

MyEntityType2 myEntityType2 = new MyEntityType2 ();
myEntityType2.copyData(myEntityType1); //access the data from the other object
//and retrieves the id 80253. myEntityType2 now has 80253 as ID.

Now this is where I get a unique constraint SQL error. Im trying to insert a ID which already exists and the changes are automatically rolled back (the old entity is no longer deleted). This happens after I see in my logger that toplink has deleted the records of the old entity.

Does anyone know how this happens, and why its not working? For the record Ive tried merging, closing, clearing of the entityManager, but nothing seems to work.

It seems to me that JPA might do some bad caching or something. I hope someone has a good answer for me! =)

Update: Theres no longer an issue with unique ID constraints, but I create a new subclass with the same primary key which has been deleted I get the following exception:

Exception Description: Trying to invoke [setApprovedWhen] on the object [null].  The 
number of actual and formal parameters differs, or an unwrapping conversion has failed.
Internal Exception: java.lang.IllegalArgumentException: object is not an instance of 
declaring class

It seems to me that it wont let me change the object into a different subclass?

Best Solution

EDIT: Try with explicitly start and commit transaction.

Deleting an entity is as simple as calling EntityManager method remove(Object entity) as the following example shows. The entity you delete must be managed: that is, it must have been previously read in the current persistence context.

myEntityType1 = getEntityManager().find(MyEntityType1.class, 80253);

When the transaction is completed, or you call EntityManager method flush(), the entity will be deleted. In a container managed persistence context the transaction boundaries will be controlled by the container.

Related Question