DATAJPA-1818 - Polishing.

Reorder methods. Fix Javadoc.

Original pull request: #435.
This commit is contained in:
Mark Paluch
2020-11-25 14:02:44 +01:00
parent 296cef790f
commit 76f216d073
2 changed files with 46 additions and 56 deletions

View File

@@ -77,10 +77,9 @@ public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>,
<S extends T> 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<T, ID> extends PagingAndSortingRepository<T, ID>,
default void deleteInBatch(Iterable<T> 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<T> 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<ID> ids);

View File

@@ -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<T, ID> implements JpaRepositoryImplementation<T
private @Nullable CrudMethodMetadata metadata;
private EscapeCharacter escapeCharacter = EscapeCharacter.DEFAULT;
private static <T> Collection<T> toCollection(Iterable<T> ts) {
if (ts instanceof Collection) {
return (Collection<T>) ts;
}
List<T> tCollection = new ArrayList<T>();
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<T, ID> implements JpaRepositoryImplementation<T
em.remove(em.contains(entity) ? entity : em.merge(entity));
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#deleteAllById(java.lang.Iterable)
*/
@Override
public void deleteAllById(Iterable<? extends ID> 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<ID> 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<T, ID> implements JpaRepositoryImplementation<T
}
}
@Override
public void deleteAllById(Iterable<? extends ID> 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<T, ID> implements JpaRepositoryImplementation<T
.executeUpdate();
}
@Override
public void deleteAllByIdInBatch(Iterable<ID> 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<T, ID> implements JpaRepositoryImplementation<T
return results;
}
Collection<ID> idCollection = toCollection(ids);
Collection<ID> idCollection = Streamable.of(ids).toList();
ByIdsSpecification<T> specification = new ByIdsSpecification<T>(entityInformation);
TypedQuery<T> query = getQuery(specification, Sort.unsorted());