Closed as not planned
Description
I think if the entity was retrieved using the find()
in the delete method of SimpleJpaRepository, it can be considered as already managed by the persistence context.
However, I'm curious if there is a need to check if the entity exists in the persistence context using the contains()
method before executing the remove()
method below.
@Override
@Transactional
@SuppressWarnings("unchecked")
public void delete(T entity) {
Assert.notNull(entity, "Entity must not be null");
if (entityInformation.isNew(entity)) {
return;
}
Class<?> type = ProxyUtils.getUserClass(entity);
T existing = (T) entityManager.find(type, entityInformation.getId(entity));
// if the entity to be deleted doesn't exist, delete is a NOOP
if (existing == null) {
return;
}
entityManager.remove(entityManager.contains(entity) ? entity : entityManager.merge(entity));
}
If so, isn't it unnecessary to call contains()
?