Skip to content

Commit ef278f5

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

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
@@ -81,6 +81,7 @@
8181
* @author Jesse Wouters
8282
* @author Greg Turnquist
8383
* @author Yanming Zhou
84+
* @author Ernst-Jan van der Laan
8485
*/
8586
@Repository
8687
@Transactional(readOnly = true)
@@ -226,13 +227,22 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
226227
return;
227228
}
228229

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

232-
Query query = em.createQuery(queryString);
233-
query.setParameter("ids", ids);
241+
Query query = em.createQuery(queryString);
242+
query.setParameter("ids", ids);
234243

235-
query.executeUpdate();
244+
query.executeUpdate();
245+
}
236246
}
237247

238248
/*

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)