From e85ad508a38f6e0b4d484a8d82cfc45bfc0f7c85 Mon Sep 17 00:00:00 2001 From: Ernst-Jan van der Laan Date: Fri, 21 Jan 2022 15:45:46 +0100 Subject: [PATCH] Support compound IdClass ID's when calling deleteAllByIdInBatch. Convert ID's to entities and pass them to deleteAllInBatch. Closes #2414 Original pull request #2419 --- .../support/SimpleJpaRepository.java | 20 ++++++++++---- .../RepositoryWithCompositeKeyTests.java | 27 ++++++++++++++++++- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index 5f8a82715..10b5c5730 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -81,6 +81,7 @@ import org.springframework.util.Assert; * @author Jesse Wouters * @author Greg Turnquist * @author Yanming Zhou + * @author Ernst-Jan van der Laan */ @Repository @Transactional(readOnly = true) @@ -226,13 +227,22 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation entities = new ArrayList<>(); + ids.forEach(id -> entities.add(getById(id))); + deleteAllInBatch(entities); + } else { + String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(), + entityInformation.getIdAttribute().getName()); - Query query = em.createQuery(queryString); - query.setParameter("ids", ids); + Query query = em.createQuery(queryString); + query.setParameter("ids", ids); - query.executeUpdate(); + query.executeUpdate(); + } } /* diff --git a/src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java b/src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java index c055a41b9..c6260fac9 100644 --- a/src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java @@ -20,9 +20,10 @@ import static org.assertj.core.api.Assertions.*; import java.util.Arrays; import java.util.List; +import javax.persistence.EntityManager; + import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -47,6 +48,7 @@ import org.springframework.transaction.annotation.Transactional; * @author Thomas Darimont * @author Mark Paluch * @author Jens Schauder + * @author Ernst-Jan van der Laan */ @ExtendWith(SpringExtension.class) @ContextConfiguration(classes = SampleConfig.class) @@ -55,6 +57,7 @@ public class RepositoryWithCompositeKeyTests { @Autowired EmployeeRepositoryWithIdClass employeeRepositoryWithIdClass; @Autowired EmployeeRepositoryWithEmbeddedId employeeRepositoryWithEmbeddedId; + @Autowired EntityManager em; /** * @see Final JPA 2.0 @@ -126,6 +129,28 @@ public class RepositoryWithCompositeKeyTests { assertThat(page.getTotalElements()).isEqualTo(1L); } + @Test // DATAJPA-2414 + void shouldSupportDeleteAllByIdInBatchWithIdClass() throws Exception { + + IdClassExampleDepartment dep = new IdClassExampleDepartment(); + dep.setName("TestDepartment"); + dep.setDepartmentId(-1); + + IdClassExampleEmployee emp = new IdClassExampleEmployee(); + emp.setDepartment(dep); + emp = employeeRepositoryWithIdClass.save(emp); + + IdClassExampleEmployeePK key = new IdClassExampleEmployeePK(emp.getEmpId(), dep.getDepartmentId()); + assertThat(employeeRepositoryWithIdClass.findById(key)).isNotEmpty(); + + employeeRepositoryWithIdClass.deleteAllByIdInBatch(Arrays.asList(key)); + + em.flush(); + em.clear(); + + assertThat(employeeRepositoryWithIdClass.findById(key)).isEmpty(); + } + @Test // DATAJPA-497 void sortByEmbeddedPkFieldInCompositePkWithEmbeddedIdInQueryDsl() {