Skip to content

Commit ae5c553

Browse files
committed
fix: support compound IdClass ID's when calling deleteAllByIdInBatch
Convert ID's to entities and pass them to deleteAllInBatch. Issue: spring-projects#2414
1 parent 7f8d9e8 commit ae5c553

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
* @author Jesse Wouters
7878
* @author Greg Turnquist
7979
* @author Yanming Zhou
80+
* @author Ernst-Jan van der Laan
8081
*/
8182
@Repository
8283
@Transactional(readOnly = true)
@@ -222,13 +223,22 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
222223
return;
223224
}
224225

225-
String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(),
226-
entityInformation.getIdAttribute().getName());
226+
if (entityInformation.hasCompositeId()) {
227+
// XXX Hibernate just creates an empty Entity when doing the getById.
228+
// Others might do a select right away causing a big performance penalty.
229+
// See JavaDoc for getById.
230+
List<T> entities = new ArrayList<>();
231+
ids.forEach(id -> entities.add(getById(id)));
232+
deleteAllInBatch(entities);
233+
} else {
234+
String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(),
235+
entityInformation.getIdAttribute().getName());
227236

228-
Query query = em.createQuery(queryString);
229-
query.setParameter("ids", ids);
237+
Query query = em.createQuery(queryString);
238+
query.setParameter("ids", ids);
230239

231-
query.executeUpdate();
240+
query.executeUpdate();
241+
}
232242
}
233243

234244
/*

src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
import java.util.Arrays;
2121
import java.util.List;
2222

23+
import javax.persistence.EntityManager;
24+
2325
import org.junit.jupiter.api.Test;
2426
import org.junit.jupiter.api.extension.ExtendWith;
25-
2627
import org.springframework.beans.factory.annotation.Autowired;
2728
import org.springframework.data.domain.Page;
2829
import org.springframework.data.domain.PageRequest;
@@ -47,6 +48,7 @@
4748
* @author Thomas Darimont
4849
* @author Mark Paluch
4950
* @author Jens Schauder
51+
* @author Ernst-Jan van der Laan
5052
*/
5153
@ExtendWith(SpringExtension.class)
5254
@ContextConfiguration(classes = SampleConfig.class)
@@ -55,6 +57,7 @@ public class RepositoryWithCompositeKeyTests {
5557

5658
@Autowired EmployeeRepositoryWithIdClass employeeRepositoryWithIdClass;
5759
@Autowired EmployeeRepositoryWithEmbeddedId employeeRepositoryWithEmbeddedId;
60+
@Autowired EntityManager em;
5861

5962
/**
6063
* @see <a href="download.oracle.com/otn-pub/jcp/persistence-2_1-fr-eval-spec/JavaPersistence.pdf">Final JPA 2.0
@@ -126,6 +129,28 @@ void shouldSupportFindAllWithPageableAndEntityWithIdClass() throws Exception {
126129
assertThat(page.getTotalElements()).isEqualTo(1L);
127130
}
128131

132+
@Test
133+
void shouldSupportDeleteAllByIdInBatchWithIdClass() throws Exception {
134+
135+
IdClassExampleDepartment dep = new IdClassExampleDepartment();
136+
dep.setName("TestDepartment");
137+
dep.setDepartmentId(-1);
138+
139+
IdClassExampleEmployee emp = new IdClassExampleEmployee();
140+
emp.setDepartment(dep);
141+
emp = employeeRepositoryWithIdClass.save(emp);
142+
143+
IdClassExampleEmployeePK key = new IdClassExampleEmployeePK(emp.getEmpId(), dep.getDepartmentId());
144+
assertThat(employeeRepositoryWithIdClass.findById(key)).isNotEmpty();
145+
146+
employeeRepositoryWithIdClass.deleteAllByIdInBatch(Arrays.asList(key));
147+
148+
em.flush();
149+
em.clear();
150+
151+
assertThat(employeeRepositoryWithIdClass.findById(key)).isEmpty();
152+
}
153+
129154
@Test // DATAJPA-497
130155
void sortByEmbeddedPkFieldInCompositePkWithEmbeddedIdInQueryDsl() {
131156

0 commit comments

Comments
 (0)