Skip to content

Commit 7f28d25

Browse files
committed
DATAJPA-1818 - Add JpaRepository.deleteAllByIdInBatch(Iterable<ID> ids).
1 parent 12c6a8d commit 7f28d25

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

src/main/java/org/springframework/data/jpa/repository/JpaRepository.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>,
8484
*/
8585
void deleteInBatch(Iterable<T> entities);
8686

87+
88+
/**
89+
* Deletes the entities identified by the given ids using a single {@link Query}.
90+
*
91+
* This kind of operation leaves JPAs first level cache and the database out of sync.
92+
* Consider flushing the `EntityManager` before calling this method.
93+
*
94+
* @param ids
95+
*
96+
* @since 3.0
97+
*/
98+
void deleteAllByIdInBatch(Iterable<ID> ids);
99+
87100
/**
88101
* Deletes all entities in a batch call.
89102
*/

src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public abstract class QueryUtils {
8686

8787
public static final String COUNT_QUERY_STRING = "select count(%s) from %s x";
8888
public static final String DELETE_ALL_QUERY_STRING = "delete from %s x";
89+
public static final String DELETE_ALL_QUERY_BY_ID_STRING = "delete from %s x where %s in :ids";
8990

9091
// Used Regex/Unicode categories (see https://www.unicode.org/reports/tr18/#General_Category_Property):
9192
// Z Separator

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,23 @@ public void deleteInBatch(Iterable<T> entities) {
246246
.executeUpdate();
247247
}
248248

249+
@Override
250+
public void deleteAllByIdInBatch(Iterable<ID> ids) {
251+
252+
Assert.notNull(ids, "Ids must not be null!");
253+
254+
if (!ids.iterator().hasNext()) {
255+
return;
256+
}
257+
258+
String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(), entityInformation.getIdAttribute().getName());
259+
260+
Query query = em.createQuery(queryString);
261+
query.setParameter("ids", ids);
262+
263+
query.executeUpdate();
264+
}
265+
249266
/*
250267
* (non-Javadoc)
251268
* @see org.springframework.data.repository.Repository#deleteAll()

src/test/java/org/springframework/data/jpa/repository/support/JpaRepositoryTests.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,24 @@ void executesExistsForEntityWithIdClass() {
114114
assertThat(idClassRepository.existsById(id)).isTrue();
115115
}
116116

117-
private static interface SampleEntityRepository extends JpaRepository<SampleEntity, SampleEntityPK> {
117+
@Test // DATAJPA-1818
118+
void deleteAllByIdInBatch() {
119+
120+
SampleEntity one = new SampleEntity("one", "eins");
121+
SampleEntity two = new SampleEntity("two", "zwei");
122+
SampleEntity three = new SampleEntity("three", "drei");
123+
repository.saveAll(Arrays.asList(one, two, three));
124+
repository.flush();
125+
126+
repository.deleteAllByIdInBatch(Arrays.asList(new SampleEntityPK("one", "eins"),new SampleEntityPK("three", "drei")));
127+
assertThat(repository.findAll()).containsExactly(two);
128+
}
129+
130+
private interface SampleEntityRepository extends JpaRepository<SampleEntity, SampleEntityPK> {
118131

119132
}
120133

121-
private static interface SampleWithIdClassRepository
134+
private interface SampleWithIdClassRepository
122135
extends CrudRepository<PersistableWithIdClass, PersistableWithIdClassPK> {
123136

124137
}

0 commit comments

Comments
 (0)