Skip to content

Commit 2bbbe70

Browse files
committed
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.
1 parent c5b6e9f commit 2bbbe70

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,16 @@ public void delete(T entity) {
196196

197197
Class<?> type = ProxyUtils.getUserClass(entity);
198198

199-
T existing = (T) entityManager.find(type, entityInformation.getId(entity));
200-
201-
// if the entity to be deleted doesn't exist, delete is a NOOP
202-
if (existing == null) {
199+
if (entityManager.contains(entity)) {
200+
entityManager.remove(entity);
203201
return;
204202
}
205203

206-
entityManager.remove(entityManager.contains(entity) ? entity : entityManager.merge(entity));
204+
// if the entity to be deleted doesn't exist, delete is a NOOP
205+
T existing = (T) entityManager.find(type, entityInformation.getId(entity));
206+
if (existing != null) {
207+
entityManager.remove(entityManager.merge(entity));
208+
}
207209
}
208210

209211
@Override

0 commit comments

Comments
 (0)