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 0fbc14f20e..b088004ae8 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 @@ * @author Jesse Wouters * @author Greg Turnquist * @author Yanming Zhou + * @author Ernst-Jan van der Laan */ @Repository @Transactional(readOnly = true) @@ -226,13 +227,22 @@ public void deleteAllByIdInBatch(Iterable ids) { return; } - String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(), - entityInformation.getIdAttribute().getName()); + if (entityInformation.hasCompositeId()) { + // XXX Hibernate just creates an empty Entity when doing the getById. + // Others might do a select right away causing a big performance penalty. + // See JavaDoc for getById. + List 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 c055a41b90..c6260fac94 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 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 @@ * @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 @@ void shouldSupportFindAllWithPageableAndEntityWithIdClass() throws Exception { 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() {