From d1f87cdd1b73a239c11a0710aa058c6da323e35b Mon Sep 17 00:00:00 2001 From: sami355-24 Date: Sat, 24 Aug 2024 15:16:42 +0900 Subject: [PATCH 1/6] Add test to verify if entities passed to deleteAllInBatch are removed from the persistence context. Added a test case to verify whether entities passed as arguments to the deleteAllInBatch method are properly removed from the persistence context in a JPA repository. --- .../data/jpa/repository/UserRepositoryTests.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java index 225336983e..d2065a7d77 100644 --- a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java +++ b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java @@ -3416,6 +3416,20 @@ void mergeWithNativeStatement() { .map(User::getAge).contains(30); } + @Test // GH-3360 + void removeWhenDeleteAllInBatchWithEntities() { + + flushTestUsers(); + List userList = repository.findAll(); + + repository.deleteAllInBatch(userList); + + userList.forEach(user -> + assertThat(em.contains(user)).isFalse() + ); + + } + private Page executeSpecWithSort(Sort sort) { flushTestUsers(); From 93ee90b0c9a507b5f72addb257f5f7dcc33a470a Mon Sep 17 00:00:00 2001 From: sami355-24 Date: Sat, 24 Aug 2024 16:13:17 +0900 Subject: [PATCH 2/6] Add implementation to ensure entities passed to deleteAllInBatch are removed from the persistence context. Implemented logic to ensure that entities passed as arguments to the deleteAllInBatch method are properly removed from the persistence context in a JPA repository. Added code to clear the EntityManager to prevent single delete queries from being issued to the database. --- .../data/jpa/repository/support/SimpleJpaRepository.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index bda460e3c5..7a552fefc1 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -275,6 +275,15 @@ public void deleteAllInBatch(Iterable entities) { return; } + for (T entity : entities) { + if (entityManager.contains(entity)) { + entityManager.remove(entity); + entityManager.detach(entity); + } + } + + entityManager.clear(); + applyAndBind(getQueryString(DELETE_ALL_QUERY_STRING, entityInformation.getEntityName()), entities, entityManager) .executeUpdate(); } From 21a130bce307c83c23699dac0dc85ca5a5916693 Mon Sep 17 00:00:00 2001 From: sami355-24 Date: Sat, 24 Aug 2024 16:18:56 +0900 Subject: [PATCH 3/6] Add test to verify if entity IDs passed to deleteAllInBatch are correctly removed from the persistence context. Added a test case to verify whether entity IDs passed as arguments to the deleteAllInBatch method are properly removed from the persistence context in a JPA repository. --- .../data/jpa/repository/UserRepositoryTests.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java index d2065a7d77..dda58dc53b 100644 --- a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java +++ b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java @@ -3430,6 +3430,21 @@ void removeWhenDeleteAllInBatchWithEntities() { } + @Test // GH-3360 + void removeWhenDeleteAllByIdInBatch() { + + flushTestUsers(); + List userList = repository.findAll(); + List userIds = userList.stream().map(User::getId).toList(); + + repository.deleteAllByIdInBatch(userIds); + + userList.forEach(user -> + assertThat(em.contains(user)).isFalse() + ); + + } + private Page executeSpecWithSort(Sort sort) { flushTestUsers(); From 7fe3f5c9d83ae2d55f301a01f3cee287a70615f0 Mon Sep 17 00:00:00 2001 From: sami355-24 Date: Sat, 24 Aug 2024 16:46:04 +0900 Subject: [PATCH 4/6] Add implementation to ensure entities' IDs passed to deleteAllByIdInBatch are properly handled in the persistence context. Implemented logic to ensure that entities' IDs passed as arguments to the deleteAllByIdInBatch method are properly handled and removed from the persistence context in a JPA repository. Added code to clear the EntityManager to prevent single delete queries from being issued to the database. --- .../data/jpa/repository/support/SimpleJpaRepository.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index 7a552fefc1..2c18f88bbf 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -237,6 +237,15 @@ public void deleteAllByIdInBatch(Iterable ids) { deleteAllInBatch(entities); } else { + ids.forEach(id -> { + T entity = entityManager.find(entityInformation.getJavaType(), id); + if (entity != null) { + entityManager.remove(entity); + } + }); + + entityManager.clear(); + String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(), entityInformation.getIdAttribute().getName()); From 723ad5147cbc69eaa7d9332095ded01961174e39 Mon Sep 17 00:00:00 2001 From: sami355-24 Date: Sat, 24 Aug 2024 16:54:04 +0900 Subject: [PATCH 5/6] Add author name --- .../data/jpa/repository/support/SimpleJpaRepository.java | 1 + .../springframework/data/jpa/repository/UserRepositoryTests.java | 1 + 2 files changed, 2 insertions(+) diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index 2c18f88bbf..5e46889dea 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -93,6 +93,7 @@ * @author Ernst-Jan van der Laan * @author Diego Krupitza * @author Seol-JY + * @author SangMin Lee */ @Repository @Transactional(readOnly = true) diff --git a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java index dda58dc53b..a240e32116 100644 --- a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java +++ b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java @@ -97,6 +97,7 @@ * @author Geoffrey Deremetz * @author Krzysztof Krason * @author Yanming Zhou + * @author SangMin Lee */ @ExtendWith(SpringExtension.class) @ContextConfiguration("classpath:application-context.xml") From 08dbd8cb468341f796d0ffe3425ca6948375e3af Mon Sep 17 00:00:00 2001 From: sami355-24 Date: Thu, 29 Aug 2024 21:26:53 +0900 Subject: [PATCH 6/6] Delete code that invokes unnecessary methods Since the clear method on the entityManager is called after the loop, the code that detaches entities after calling remove inside the loop is unnecessary. --- .../data/jpa/repository/support/SimpleJpaRepository.java | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index 5e46889dea..27dc8a54ce 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -288,7 +288,6 @@ public void deleteAllInBatch(Iterable entities) { for (T entity : entities) { if (entityManager.contains(entity)) { entityManager.remove(entity); - entityManager.detach(entity); } }