1

I've an Entity (with a primary key that is not generated by a sequence) like this in a Spring Data JPA/Eclipselink environment :

@Entity
@Table(name="MY_ENTITY")
public class MyEntity implements Serializable {

    @Id 
    @Column(insertable=true, updatable=true, nullable=false)
    private String propertyid;

    \\other columns
}

and I'm trying to delete a row from the table and reinsert it (with the same primary key).

My approach is to call deleteAll() to clean the table and then save() the new Entity :

    @Transactional
    public void deleteAndSave(MyEntity entity) {
        propertyInfoRepository.deleteAll();
        propertyInfoRepository.flush(); // <- having it or not, nothing changes
        propertyInfoRepository.save(entity);
    }       

but this gives me this error :

    Caused by: java.lang.IllegalArgumentException: Cannot merge an entity that has been removed: com.xxx.MyEntity@1f28c51
at org.eclipse.persistence.internal.sessions.MergeManager.registerObjectForMergeCloneIntoWorkingCopy(MergeManager.java:912)
at org.eclipse.persistence.internal.sessions.MergeManager.mergeChangesOfCloneIntoWorkingCopy(MergeManager.java:494)
at org.eclipse.persistence.internal.sessions.MergeManager.mergeChanges(MergeManager.java:271)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.mergeCloneWithReferences(UnitOfWorkImpl.java:3495)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.mergeCloneWithReferences(RepeatableWriteUnitOfWork.java:378)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.mergeCloneWithReferences(UnitOfWorkImpl.java:3455)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.mergeInternal(EntityManagerImpl.java:486)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.merge(EntityManagerImpl.java:463)
....

What am I doing wrong? I do not understand why it is trying to merge the entity instead of simply reinsert it after its deletion.

Thanks for your help!

V G
  • 18,822
  • 6
  • 51
  • 89
lincetto
  • 972
  • 2
  • 13
  • 28

2 Answers2

1

Directly to answer your question:

The problem is that the entity that you try to save has already a persistent identity, i.e an ID, which is why your repository will try to merge, and not to persist the entity.

If you see this question it seems that it is triggered (at least) on the level of the Spring Repository, so you might consider overriding the save method of the repository and test whether the problem is still there.

Community
  • 1
  • 1
V G
  • 18,822
  • 6
  • 51
  • 89
-1

JPA EntityManager keeps track of the state of each managed entity. In your case, you delete the entity and then try to merge it, which raises the exception. I can't tell if your approach is correct (seems weird to delete and then merge) since you don't provide the whole picture but you can try the following:

Assuming em is your EntityManager and entity your entity:

em.remove(entity); //This will perform the delete
MyEntity detachedEntity = em.detach(entity); //Gets a detached copy of the entity, EM will not operated on this unless told to do so (see below)
detachedEntity.setId(null) // Avoid duplicate key violations; Optional since you are deleting the original entity
em.persist(detachedEntity); // This will perform the required insert
Tasos P.
  • 3,994
  • 2
  • 21
  • 41
  • 1
    I do not try to delete and then merge the entity. Entity is always a new instance (perhaps with the same primary key) and at the moment I call deleteAndSave method, it is not already managed. – lincetto Nov 28 '13 at 09:31