Skip to content

Commit 4f54291

Browse files
Seol-JYmp911de
authored andcommitted
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
1 parent 3ab36fa commit 4f54291

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
* @author Yanming Zhou
9494
* @author Ernst-Jan van der Laan
9595
* @author Diego Krupitza
96+
* @author Seol-JY
9697
*/
9798
@Repository
9899
@Transactional(readOnly = true)
@@ -196,14 +197,16 @@ public void delete(T entity) {
196197

197198
Class<?> type = ProxyUtils.getUserClass(entity);
198199

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) {
200+
if (entityManager.contains(entity)) {
201+
entityManager.remove(entity);
203202
return;
204203
}
205204

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

209212
@Override

0 commit comments

Comments
 (0)