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 231a50813..8e943fd7a 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 @@ -33,7 +33,6 @@ import javax.persistence.Query; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; -import javax.persistence.criteria.Order; import javax.persistence.criteria.ParameterExpression; import javax.persistence.criteria.Path; import javax.persistence.criteria.Predicate; @@ -77,6 +76,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) @@ -222,13 +222,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(); + } } /* @@ -313,8 +322,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation implements JpaRepositoryImplementation List findAll(Example example, Sort sort) { - return getQuery(new ExampleSpecification(example, escapeCharacter), example.getProbeType(), sort) - .getResultList(); + return getQuery(new ExampleSpecification(example, escapeCharacter), example.getProbeType(), sort).getResultList(); } /* 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() {