diff --git a/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java index 029f1d997..a509c304f 100644 --- a/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java @@ -77,10 +77,9 @@ public interface JpaRepository extends PagingAndSortingRepository, S saveAndFlush(S entity); /** - * Deletes the given entities in a batch which means it will create a single {@link Query}. - * - * This kind of operation leaves JPAs first level cache and the database out of sync. - * Consider flushing the `EntityManager` before calling this method. + * Deletes the given entities in a batch which means it will create a single query. This kind of operation leaves JPAs + * first level cache and the database out of sync. Consider flushing the {@link EntityManager} before calling this + * method. * * @param entities * @deprecated Use {@link #deleteAllInBatch(Iterable)} instead. @@ -89,26 +88,21 @@ public interface JpaRepository extends PagingAndSortingRepository, default void deleteInBatch(Iterable entities){deleteAllInBatch(entities);} /** - * Deletes the given entities in a batch which means it will create a single {@link Query}. - * - * This kind of operation leaves JPAs first level cache and the database out of sync. - * Consider flushing the `EntityManager` before calling this method. + * Deletes the given entities in a batch which means it will create a single query. This kind of operation leaves JPAs + * first level cache and the database out of sync. Consider flushing the {@link EntityManager} before calling this + * method. * * @param entities - * * @since 3.0 */ void deleteAllInBatch(Iterable entities); /** - * Deletes the entities identified by the given ids using a single {@link Query}. - * - * This kind of operation leaves JPAs first level cache and the database out of sync. - * Consider flushing the `EntityManager` before calling this method. + * Deletes the entities identified by the given ids using a single query. This kind of operation leaves JPAs first + * level cache and the database out of sync. Consider flushing the {@link EntityManager} before calling this method. * * @param ids - * * @since 3.0 */ void deleteAllByIdInBatch(Iterable ids); diff --git a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index 3dfea791b..13b47b534 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -54,6 +54,7 @@ import org.springframework.data.jpa.repository.query.QueryUtils; import org.springframework.data.jpa.repository.support.QueryHints.NoHints; import org.springframework.data.repository.support.PageableExecutionUtils; import org.springframework.data.util.ProxyUtils; +import org.springframework.data.util.Streamable; import org.springframework.lang.Nullable; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; @@ -88,19 +89,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation Collection toCollection(Iterable ts) { - - if (ts instanceof Collection) { - return (Collection) ts; - } - - List tCollection = new ArrayList(); - for (T t : ts) { - tCollection.add(t); - } - return tCollection; - } - /** * Creates a new {@link SimpleJpaRepository} to manage objects of the given {@link JpaEntityInformation}. * @@ -203,6 +191,42 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation ids) { + + Assert.notNull(ids, "Ids must not be null!"); + + for (ID id : ids) { + deleteById(id); + } + } + + /* + * (non-Javadoc) + * @see org.springframework.data.repository.CrudRepository#deleteAllByIdInBatch(java.lang.Iterable) + */ + @Override + public void deleteAllByIdInBatch(Iterable ids) { + + Assert.notNull(ids, "Ids must not be null!"); + + if (!ids.iterator().hasNext()) { + return; + } + + String queryString = String.format(DELETE_ALL_QUERY_BY_ID_STRING, entityInformation.getEntityName(), + entityInformation.getIdAttribute().getName()); + + Query query = em.createQuery(queryString); + query.setParameter("ids", ids); + + query.executeUpdate(); + } + /* * (non-Javadoc) * @see org.springframework.data.repository.CrudRepository#delete(java.lang.Iterable) @@ -218,16 +242,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation ids) { - - Assert.notNull(ids, "Ids must not be null!"); - - for (ID id : ids) { - deleteById(id); - } - } - /* * (non-Javadoc) * @see org.springframework.data.jpa.repository.JpaRepository#deleteInBatch(java.lang.Iterable) @@ -246,24 +260,6 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation ids) { - - Assert.notNull(ids, "Ids must not be null!"); - - if (!ids.iterator().hasNext()) { - return; - } - - String queryTemplate = DELETE_ALL_QUERY_BY_ID_STRING; - String queryString = String.format(queryTemplate, entityInformation.getEntityName(), entityInformation.getIdAttribute().getName()); - - Query query = em.createQuery(queryString); - query.setParameter("ids", ids); - - query.executeUpdate(); - } - /* * (non-Javadoc) * @see org.springframework.data.repository.Repository#deleteAll() @@ -408,7 +404,7 @@ public class SimpleJpaRepository implements JpaRepositoryImplementation idCollection = toCollection(ids); + Collection idCollection = Streamable.of(ids).toList(); ByIdsSpecification specification = new ByIdsSpecification(entityInformation); TypedQuery query = getQuery(specification, Sort.unsorted());