Skip to content

Commit dd7fbb0

Browse files
committed
DATAJPA-1818 - Implements CrudRepository.delete(Iterable<ID> ids).
1 parent 25f5e00 commit dd7fbb0

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,16 @@ public void deleteAll(Iterable<? extends T> entities) {
218218
}
219219
}
220220

221+
@Override
222+
public void deleteAllById(Iterable<? extends ID> ids) {
223+
224+
Assert.notNull(ids, "Ids must not be null!");
225+
226+
for (ID id : ids) {
227+
deleteById(id);
228+
}
229+
}
230+
221231
/*
222232
* (non-Javadoc)
223233
* @see org.springframework.data.jpa.repository.JpaRepository#deleteInBatch(java.lang.Iterable)

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

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.jpa.repository;
1717

18+
import static java.util.Arrays.*;
1819
import static org.assertj.core.api.Assertions.*;
1920
import static org.springframework.data.domain.Example.*;
2021
import static org.springframework.data.domain.ExampleMatcher.*;
@@ -151,7 +152,7 @@ void findsAllByGivenIds() {
151152

152153
flushTestUsers();
153154

154-
assertThat(repository.findAllById(Arrays.asList(firstUser.getId(), secondUser.getId()))).contains(firstUser,
155+
assertThat(repository.findAllById(asList(firstUser.getId(), secondUser.getId()))).contains(firstUser,
155156
secondUser);
156157
}
157158

@@ -166,7 +167,7 @@ void testReadByIdReturnsNullForNotFoundEntities() {
166167
@Test
167168
void savesCollectionCorrectly() throws Exception {
168169

169-
assertThat(repository.saveAll(Arrays.asList(firstUser, secondUser, thirdUser))).hasSize(3).contains(firstUser,
170+
assertThat(repository.saveAll(asList(firstUser, secondUser, thirdUser))).hasSize(3).contains(firstUser,
170171
secondUser, thirdUser);
171172
}
172173

@@ -238,27 +239,41 @@ void returnsAllIgnoreCaseSortedCorrectly() throws Exception {
238239
}
239240

240241
@Test
241-
void deleteColletionOfEntities() {
242+
void deleteCollectionOfEntities() {
242243

243244
flushTestUsers();
244245

245246
long before = repository.count();
246247

247-
repository.deleteAll(Arrays.asList(firstUser, secondUser));
248+
repository.deleteAll(asList(firstUser, secondUser));
248249

249250
assertThat(repository.existsById(firstUser.getId())).isFalse();
250251
assertThat(repository.existsById(secondUser.getId())).isFalse();
251252
assertThat(repository.count()).isEqualTo(before - 2);
252253
}
253254

254255
@Test
255-
void batchDeleteColletionOfEntities() {
256+
void batchDeleteCollectionOfEntities() {
256257

257258
flushTestUsers();
258259

259260
long before = repository.count();
260261

261-
repository.deleteInBatch(Arrays.asList(firstUser, secondUser));
262+
repository.deleteInBatch(asList(firstUser, secondUser));
263+
264+
assertThat(repository.existsById(firstUser.getId())).isFalse();
265+
assertThat(repository.existsById(secondUser.getId())).isFalse();
266+
assertThat(repository.count()).isEqualTo(before - 2);
267+
}
268+
269+
@Test // DATAJPA-1818
270+
void deleteCollectionOfEntitiesById() {
271+
272+
flushTestUsers();
273+
274+
long before = repository.count();
275+
276+
repository.deleteAllById(asList(firstUser.getId(), secondUser.getId()));
262277

263278
assertThat(repository.existsById(firstUser.getId())).isFalse();
264279
assertThat(repository.existsById(secondUser.getId())).isFalse();
@@ -603,7 +618,7 @@ void executesQueryMethodWithDeepTraversalCorrectly() throws Exception {
603618

604619
firstUser.setManager(secondUser);
605620
thirdUser.setManager(firstUser);
606-
repository.saveAll(Arrays.asList(firstUser, thirdUser));
621+
repository.saveAll(asList(firstUser, thirdUser));
607622

608623
assertThat(repository.findByManagerLastname("Arrasz")).containsOnly(firstUser);
609624
assertThat(repository.findByManagerLastname("Gierke")).containsOnly(thirdUser);
@@ -616,7 +631,7 @@ void executesFindByColleaguesLastnameCorrectly() throws Exception {
616631

617632
firstUser.addColleague(secondUser);
618633
thirdUser.addColleague(firstUser);
619-
repository.saveAll(Arrays.asList(firstUser, thirdUser));
634+
repository.saveAll(asList(firstUser, thirdUser));
620635

621636
assertThat(repository.findByColleaguesLastname(secondUser.getLastname())).containsOnly(firstUser);
622637

@@ -1178,7 +1193,7 @@ void findByElementCollectionAttribute() {
11781193

11791194
flushTestUsers();
11801195

1181-
List<User> result = repository.findByAttributesIn(new HashSet<>(Arrays.asList("cool", "hip")));
1196+
List<User> result = repository.findByAttributesIn(new HashSet<>(asList("cool", "hip")));
11821197

11831198
assertThat(result).containsOnly(firstUser, secondUser);
11841199
}
@@ -1731,7 +1746,7 @@ void findAllByExampleWithAssociation() {
17311746

17321747
firstUser.setManager(secondUser);
17331748
thirdUser.setManager(firstUser);
1734-
repository.saveAll(Arrays.asList(firstUser, thirdUser));
1749+
repository.saveAll(asList(firstUser, thirdUser));
17351750

17361751
User manager = new User();
17371752
manager.setLastname("Arrasz");
@@ -1854,7 +1869,7 @@ void findAllByExampleWithIncludeNull() {
18541869
fifthUser.setFirstname(firstUser.getFirstname());
18551870
fifthUser.setLastname(firstUser.getLastname());
18561871

1857-
repository.saveAll(Arrays.asList(firstUser, fifthUser));
1872+
repository.saveAll(asList(firstUser, fifthUser));
18581873

18591874
User prototype = new User();
18601875
prototype.setFirstname(firstUser.getFirstname());
@@ -2242,7 +2257,7 @@ void findByElementCollectionInAttributeIgnoreCase() {
22422257

22432258
flushTestUsers();
22442259

2245-
List<User> result = repository.findByAttributesIgnoreCaseIn(new HashSet<>(Arrays.asList("cOOl", "hIP")));
2260+
List<User> result = repository.findByAttributesIgnoreCaseIn(new HashSet<>(asList("cOOl", "hIP")));
22462261

22472262
assertThat(result).containsOnly(firstUser, secondUser);
22482263
}
@@ -2256,7 +2271,7 @@ void findByElementCollectionNotInAttributeIgnoreCase() {
22562271

22572272
flushTestUsers();
22582273

2259-
List<User> result = repository.findByAttributesIgnoreCaseNotIn(Arrays.asList("CooL", "HIp"));
2274+
List<User> result = repository.findByAttributesIgnoreCaseNotIn(asList("CooL", "HIp"));
22602275

22612276
assertThat(result).containsOnly(thirdUser);
22622277
}
@@ -2284,7 +2299,7 @@ void findByElementCollectionInAttributeIgnoreCaseWithNulls() {
22842299

22852300
flushTestUsers();
22862301

2287-
List<User> result = repository.findByAttributesIgnoreCaseIn(Arrays.asList("cOOl", null));
2302+
List<User> result = repository.findByAttributesIgnoreCaseIn(asList("cOOl", null));
22882303

22892304
assertThat(result).containsOnly(firstUser);
22902305
}

0 commit comments

Comments
 (0)