I am trying to obtain the list of deleted entities in a database operation, to implement a custom auditing. Unfortunately it is not enough to just override the remove
method of the EntityManager
, because of cascading deletes.
I was able to find new and updated entities by utilizing the EntityManager
and its UnitOfWorkChangeSet
. Curiously the list of deleted entities is always empty.
I suspect the way the client invoke the method to be problematic, but am not quite sure. The client removes items from a collection of entities of an entity by removing them on the object and than calling the persist
method with the entity holding the collection. This approach works in terms of removing the objects from the database by utilizing the cascade mechanism, but I can't find the removed entities in the list of deleted objects.
Accessing deleted entities in changeset:
Map<ObjectChangeSet, ObjectChangeSet> deletedObjects =
entityManager.unwrap(UnitOfWork.class)
.getCurrentChanges()
// returns always an empty list
.getDeletedObjects();
return deletedObjects.values().stream()
.map(v -> v.getUnitOfWorkClone())
.collect(Collectors.toList());
This code is executed every time the persist
method is invoked. It always returns an empty list.
Why does getDeletedObjects()
always return an empty list and when does an entity get added to this list?