Add another cc.ryanc.halo.repository.base.BaseRepository.findAllByIdIn(java.lang.Iterable<ID>, org.springframework.data.domain.Pageable) common method

pull/137/head
johnniang 2019-04-03 02:10:21 +08:00
parent 6e9936119d
commit e64298209e
2 changed files with 77 additions and 8 deletions

View File

@ -1,5 +1,7 @@
package cc.ryanc.halo.repository.base;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
@ -18,7 +20,7 @@ import java.util.List;
public interface BaseRepository<DOMAIN, ID> extends JpaRepository<DOMAIN, ID> {
/**
* Finds all domain by id list and the specified sort.
* Finds all domain by id list.
*
* @param ids id list of domain must not be null
* @param sort the specified sort must not be null
@ -27,6 +29,16 @@ public interface BaseRepository<DOMAIN, ID> extends JpaRepository<DOMAIN, ID> {
@NonNull
List<DOMAIN> findAllByIdIn(@NonNull Iterable<ID> ids, @NonNull Sort sort);
/**
* Finds all domain by domain id list.
*
* @param ids must not be null
* @param pageable must not be null
* @return a list of domains
*/
@NonNull
Page<DOMAIN> findAllByIdIn(@NonNull Iterable<ID> ids, @NonNull Pageable pageable);
/**
* Deletes by id list.
*

View File

@ -1,10 +1,14 @@
package cc.ryanc.halo.repository.base;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@ -45,23 +49,45 @@ public class BaseRepositoryImpl<DOMAIN, ID> extends SimpleJpaRepository<DOMAIN,
@Override
public List<DOMAIN> findAllByIdIn(Iterable<ID> ids, Sort sort) {
Assert.notNull(ids, "The given Iterable of Id's must not be null!");
Assert.notNull(sort, "Sort info must nto be null");
log.debug("Customized findAllById method was invoked");
if (!ids.iterator().hasNext()) {
return Collections.emptyList();
}
if (!this.entityInformation.hasCompositeId()) {
ByIdsSpecification<DOMAIN> specification = new ByIdsSpecification<>(this.entityInformation);
TypedQuery<DOMAIN> query = super.getQuery(specification, sort);
return query.setParameter(specification.parameter, ids).getResultList();
} else {
if (entityInformation.hasCompositeId()) {
List<DOMAIN> results = new ArrayList<>();
ids.forEach(id -> super.findById(id).ifPresent(results::add));
return results;
}
ByIdsSpecification<DOMAIN> specification = new ByIdsSpecification<>(entityInformation);
TypedQuery<DOMAIN> query = super.getQuery(specification, sort);
return query.setParameter(specification.parameter, ids).getResultList();
}
@Override
public Page<DOMAIN> findAllByIdIn(Iterable<ID> ids, Pageable pageable) {
Assert.notNull(ids, "The given Iterable of Id's must not be null!");
Assert.notNull(pageable, "Page info must nto be null");
if (!ids.iterator().hasNext()) {
return new PageImpl<>(Collections.emptyList());
}
if (entityInformation.hasCompositeId()) {
throw new UnsupportedOperationException("Unsupported find all by composite id with page info");
}
ByIdsSpecification<DOMAIN> specification = new ByIdsSpecification<>(entityInformation);
TypedQuery<DOMAIN> query = super.getQuery(specification, pageable).setParameter(specification.parameter, ids);
TypedQuery<Long> countQuery = getCountQuery(specification, getDomainClass()).setParameter(specification.parameter, ids);
return pageable.isUnpaged() ?
new PageImpl<>(query.getResultList())
: readPage(query, getDomainClass(), pageable, countQuery);
}
/**
@ -84,6 +110,17 @@ public class BaseRepositoryImpl<DOMAIN, ID> extends SimpleJpaRepository<DOMAIN,
return domains.size();
}
protected <S extends DOMAIN> Page<S> readPage(TypedQuery<S> query, Class<S> domainClass, Pageable pageable, TypedQuery<Long> countQuery) {
if (pageable.isPaged()) {
query.setFirstResult((int) pageable.getOffset());
query.setMaxResults(pageable.getPageSize());
}
return PageableExecutionUtils.getPage(query.getResultList(), pageable,
() -> executeCountQuery(countQuery));
}
private static final class ByIdsSpecification<T> implements Specification<T> {
private static final long serialVersionUID = 1L;
private final JpaEntityInformation<T, ?> entityInformation;
@ -101,4 +138,24 @@ public class BaseRepositoryImpl<DOMAIN, ID> extends SimpleJpaRepository<DOMAIN,
return path.in(this.parameter);
}
}
/**
* Executes a count query and transparently sums up all values returned.
*
* @param query must not be {@literal null}.
* @return
*/
private static long executeCountQuery(TypedQuery<Long> query) {
Assert.notNull(query, "TypedQuery must not be null!");
List<Long> totals = query.getResultList();
long total = 0L;
for (Long element : totals) {
total += element == null ? 0 : element;
}
return total;
}
}