I am using JPA (with Hibernate as the JPA provider). I have an operation to remove an entity, and it seems that if I detach the entity and immediately merge it an exception is thrown, i.e.
em.find(entity.class, entitiy.getId())
em.detach(entity)
em.merge(entity)
will cause errors while simply
em.find(entity.class, entitiy.getId())
Will pass flawlessly. Is there something that I am missing regarding detach and merge? I didn't do anything on the entity itself, it wasn't change, so the merge should not change anything.
I can elaborate more on my specific case if needed.
EDIT
As @rmertins pointed out, I should have used the return value from the merge, as merge returns the merge entity while the entity we have used as a parameter stays detached. This works:
em.find(entity.class, entitiy.getId())
em.detach(entity)
entity = em.merge(entity)