Skip to content

Commit 76f216d

Browse files
committed
DATAJPA-1818 - Polishing.
Reorder methods. Fix Javadoc. Original pull request: #435.
1 parent 296cef7 commit 76f216d

File tree

2 files changed

+44
-54
lines changed

2 files changed

+44
-54
lines changed

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,9 @@ public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>,
7777
<S extends T> S saveAndFlush(S entity);
7878

7979
/**
80-
* Deletes the given entities in a batch which means it will create a single {@link Query}.
81-
*
82-
* This kind of operation leaves JPAs first level cache and the database out of sync.
83-
* Consider flushing the `EntityManager` before calling this method.
80+
* Deletes the given entities in a batch which means it will create a single query. This kind of operation leaves JPAs
81+
* first level cache and the database out of sync. Consider flushing the {@link EntityManager} before calling this
82+
* method.
8483
*
8584
* @param entities
8685
* @deprecated Use {@link #deleteAllInBatch(Iterable)} instead.
@@ -89,26 +88,21 @@ public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>,
8988
default void deleteInBatch(Iterable<T> entities){deleteAllInBatch(entities);}
9089

9190
/**
92-
* Deletes the given entities in a batch which means it will create a single {@link Query}.
93-
*
94-
* This kind of operation leaves JPAs first level cache and the database out of sync.
95-
* Consider flushing the `EntityManager` before calling this method.
91+
* Deletes the given entities in a batch which means it will create a single query. This kind of operation leaves JPAs
92+
* first level cache and the database out of sync. Consider flushing the {@link EntityManager} before calling this
93+
* method.
9694
*
9795
* @param entities
98-
*
9996
* @since 3.0
10097
*/
10198
void deleteAllInBatch(Iterable<T> entities);
10299

103100

104101
/**
105-
* Deletes the entities identified by the given ids using a single {@link Query}.
106-
*
107-
* This kind of operation leaves JPAs first level cache and the database out of sync.
108-
* Consider flushing the `EntityManager` before calling this method.
102+
* Deletes the entities identified by the given ids using a single query. This kind of operation leaves JPAs first
103+
* level cache and the database out of sync. Consider flushing the {@link EntityManager} before calling this method.
109104
*
110105
* @param ids
111-
*
112106
* @since 3.0
113107
*/
114108
void deleteAllByIdInBatch(Iterable<ID> ids);

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

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.springframework.data.jpa.repository.support.QueryHints.NoHints;
5555
import org.springframework.data.repository.support.PageableExecutionUtils;
5656
import org.springframework.data.util.ProxyUtils;
57+
import org.springframework.data.util.Streamable;
5758
import org.springframework.lang.Nullable;
5859
import org.springframework.stereotype.Repository;
5960
import org.springframework.transaction.annotation.Transactional;
@@ -88,19 +89,6 @@ public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T
8889
private @Nullable CrudMethodMetadata metadata;
8990
private EscapeCharacter escapeCharacter = EscapeCharacter.DEFAULT;
9091

91-
private static <T> Collection<T> toCollection(Iterable<T> ts) {
92-
93-
if (ts instanceof Collection) {
94-
return (Collection<T>) ts;
95-
}
96-
97-
List<T> tCollection = new ArrayList<T>();
98-
for (T t : ts) {
99-
tCollection.add(t);
100-
}
101-
return tCollection;
102-
}
103-
10492
/**
10593
* Creates a new {@link SimpleJpaRepository} to manage objects of the given {@link JpaEntityInformation}.
10694
*
@@ -205,63 +193,71 @@ public void delete(T entity) {
205193

206194
/*
207195
* (non-Javadoc)
208-
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
196+
* @see org.springframework.data.repository.CrudRepository#deleteAllById(java.lang.Iterable)
209197
*/
210-
@Transactional
211198
@Override
212-
public void deleteAll(Iterable<? extends T> entities) {
199+
public void deleteAllById(Iterable<? extends ID> ids) {
213200

214-
Assert.notNull(entities, "Entities must not be null!");
201+
Assert.notNull(ids, "Ids must not be null!");
215202

216-
for (T entity : entities) {
217-
delete(entity);
203+
for (ID id : ids) {
204+
deleteById(id);
218205
}
219206
}
220207

208+
/*
209+
* (non-Javadoc)
210+
* @see org.springframework.data.repository.CrudRepository#deleteAllByIdInBatch(java.lang.Iterable)
211+
*/
221212
@Override
222-
public void deleteAllById(Iterable<? extends ID> ids) {
213+
public void deleteAllByIdInBatch(Iterable<ID> ids) {
223214

224215
Assert.notNull(ids, "Ids must not be null!");
225216

226-
for (ID id : ids) {
227-
deleteById(id);
217+
if (!ids.iterator().hasNext()) {
218+
return;
228219
}
220+
221+
String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(),
222+
entityInformation.getIdAttribute().getName());
223+
224+
Query query = em.createQuery(queryString);
225+
query.setParameter("ids", ids);
226+
227+
query.executeUpdate();
229228
}
230229

231230
/*
232231
* (non-Javadoc)
233-
* @see org.springframework.data.jpa.repository.JpaRepository#deleteInBatch(java.lang.Iterable)
232+
* @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable)
234233
*/
235234
@Transactional
236235
@Override
237-
public void deleteAllInBatch(Iterable<T> entities) {
236+
public void deleteAll(Iterable<? extends T> entities) {
238237

239238
Assert.notNull(entities, "Entities must not be null!");
240239

241-
if (!entities.iterator().hasNext()) {
242-
return;
240+
for (T entity : entities) {
241+
delete(entity);
243242
}
244-
245-
applyAndBind(getQueryString(DELETE_ALL_QUERY_STRING, entityInformation.getEntityName()), entities, em)
246-
.executeUpdate();
247243
}
248244

245+
/*
246+
* (non-Javadoc)
247+
* @see org.springframework.data.jpa.repository.JpaRepository#deleteInBatch(java.lang.Iterable)
248+
*/
249+
@Transactional
249250
@Override
250-
public void deleteAllByIdInBatch(Iterable<ID> ids) {
251+
public void deleteAllInBatch(Iterable<T> entities) {
251252

252-
Assert.notNull(ids, "Ids must not be null!");
253+
Assert.notNull(entities, "Entities must not be null!");
253254

254-
if (!ids.iterator().hasNext()) {
255+
if (!entities.iterator().hasNext()) {
255256
return;
256257
}
257258

258-
String queryTemplate = DELETE_ALL_QUERY_BY_ID_STRING;
259-
String queryString = String.format(queryTemplate, entityInformation.getEntityName(), entityInformation.getIdAttribute().getName());
260-
261-
Query query = em.createQuery(queryString);
262-
query.setParameter("ids", ids);
263-
264-
query.executeUpdate();
259+
applyAndBind(getQueryString(DELETE_ALL_QUERY_STRING, entityInformation.getEntityName()), entities, em)
260+
.executeUpdate();
265261
}
266262

267263
/*
@@ -408,7 +404,7 @@ public List<T> findAllById(Iterable<ID> ids) {
408404
return results;
409405
}
410406

411-
Collection<ID> idCollection = toCollection(ids);
407+
Collection<ID> idCollection = Streamable.of(ids).toList();
412408

413409
ByIdsSpecification<T> specification = new ByIdsSpecification<T>(entityInformation);
414410
TypedQuery<T> query = getQuery(specification, Sort.unsorted());

0 commit comments

Comments
 (0)