Optimize entity deletion in SimpleJpaRepository.

This change improves the performance of the delete method by first checking if the entity is already managed by the EntityManager. If so, it removes the entity directly without additional database queries. This optimization can reduce unnecessary database lookups in certain scenarios.

Closes #3564
This commit is contained in:
Seol_JY
2024-08-07 15:55:15 +09:00
committed by Mark Paluch
parent 3ab36fa705
commit 4f542915f5

View File

@@ -93,6 +93,7 @@ import org.springframework.util.Assert;
* @author Yanming Zhou
* @author Ernst-Jan van der Laan
* @author Diego Krupitza
* @author Seol-JY
*/
@Repository
@Transactional(readOnly = true)
@@ -196,14 +197,16 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
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) {
if (entityManager.contains(entity)) {
entityManager.remove(entity);
return;
}
entityManager.remove(entityManager.contains(entity) ? entity : entityManager.merge(entity));
// if the entity to be deleted doesn't exist, delete is a NOOP
T existing = (T) entityManager.find(type, entityInformation.getId(entity));
if (existing != null) {
entityManager.remove(entityManager.merge(entity));
}
}
@Override