private static <T, U, V> Map<V, List<U>> groupRelationsInternal( @Nonnull final Collection<? extends T> objects, @Nonnull final Specification<U> inSpec, @Nonnull final JpaSpecificationExecutor<U> repository, @Nonnull final Function<? super U, ? extends V> keyMapper, @Nullable final Specification<U> constraint, @Nullable final Sort sortCriteria) { Objects.requireNonNull(objects, "objects must not be null"); Objects.requireNonNull(inSpec, "inSpec must not be null"); Objects.requireNonNull(repository, "repository must not be null"); if (objects.isEmpty()) { return new HashMap<>(0); } final Specification<U> compoundSpec = constraint == null ? inSpec : Specifications.where(inSpec).and(constraint); final List<U> list = sortCriteria != null ? repository.findAll(compoundSpec, sortCriteria) : repository.findAll(compoundSpec); return F.nullSafeGroupBy(list, keyMapper); }
public static <ENTITY, REPO extends JpaRepository<ENTITY, ?> & JpaSpecificationExecutor> SpecificationBuilder<ENTITY> selectFrom(REPO repository) { SpecificationBuilder<ENTITY> builder = new SpecificationBuilder<>(); builder.repository = repository; builder.specification = new SpecificationImpl(); return builder; }
@Nonnull public static <T, U> Map<T, List<U>> groupRelations( @Nonnull final Collection<? extends T> objects, @Nonnull final SingularAttribute<? super U, T> singularAttribute, @Nonnull final JpaSpecificationExecutor<U> repository) { return groupRelations(objects, singularAttribute, repository, null, null); }
@Nonnull public static <T, U> Map<T, List<U>> groupRelations( @Nonnull final Collection<? extends T> objects, @Nonnull final SingularAttribute<? super U, T> singularAttribute, @Nonnull final JpaSpecificationExecutor<U> repository, @Nullable final Specification<U> constraint) { return groupRelations(objects, singularAttribute, repository, constraint, null); }
@Nonnull public static <T, U> Map<T, List<U>> groupRelations( @Nonnull final Collection<? extends T> objects, @Nonnull final SingularAttribute<? super U, T> singularAttribute, @Nonnull final JpaSpecificationExecutor<U> repository, @Nullable final Sort sortCriteria) { return groupRelations(objects, singularAttribute, repository, null, sortCriteria); }
@Nonnull public static <T, U> Map<T, List<U>> groupRelations( @Nonnull final Collection<? extends T> objects, @Nonnull final SingularAttribute<? super U, T> singularAttribute, @Nonnull final JpaSpecificationExecutor<U> repository, @Nullable final Specification<U> constraint, @Nullable final Sort sortCriteria) { final Specification<U> inSpec = inCollection(singularAttribute, objects); return groupRelationsInternal( objects, inSpec, repository, CriteriaUtils.jpaProperty(singularAttribute), constraint, sortCriteria); }
@Nonnull public static <ID extends Serializable, T extends HasID<ID>, U> Map<ID, List<U>> groupRelationsById( @Nonnull final Collection<? extends T> objects, @Nonnull final SingularAttribute<? super U, T> singularAttribute, @Nonnull final JpaSpecificationExecutor<U> repository) { return groupRelationsById(objects, singularAttribute, repository, null, null); }
@Nonnull public static <ID extends Serializable, T extends HasID<ID>, U> Map<ID, List<U>> groupRelationsById( @Nonnull final Collection<? extends T> objects, @Nonnull final SingularAttribute<? super U, T> singularAttribute, @Nonnull final JpaSpecificationExecutor<U> repository, @Nullable final Specification<U> constraint) { return groupRelationsById(objects, singularAttribute, repository, constraint, null); }
@Nonnull public static <ID extends Serializable, T extends HasID<ID>, U> Map<ID, List<U>> groupRelationsById( @Nonnull final Collection<? extends T> objects, @Nonnull final SingularAttribute<? super U, T> singularAttribute, @Nonnull final JpaSpecificationExecutor<U> repository, @Nullable final Specification<U> constraint, @Nullable final Sort sortCriteria) { final Specification<U> inSpec = inCollection(singularAttribute, objects); final Function<U, ID> idFunction = Functions.idOf(CriteriaUtils.jpaProperty(singularAttribute)); return groupRelationsInternal(objects, inSpec, repository, idFunction, constraint, sortCriteria); }
@Nonnull public static <ID extends Serializable, T extends HasID<ID>, U> Map<ID, List<U>> groupRelationsById( @Nonnull final Collection<ID> ids, @Nonnull final SingularAttribute<? super U, T> associationAttribute, @Nonnull final SingularAttribute<? super T, ID> idAttribute, @Nonnull final JpaSpecificationExecutor<U> repository) { return groupRelationsById(ids, associationAttribute, idAttribute, repository, null, null); }
@Nonnull public static <ID extends Serializable, T extends HasID<ID>, U> Map<ID, List<U>> groupRelationsById( @Nonnull final Collection<ID> ids, @Nonnull final SingularAttribute<? super U, T> associationAttribute, @Nonnull final SingularAttribute<? super T, ID> idAttribute, @Nonnull final JpaSpecificationExecutor<U> repository, @Nullable final Specification<U> constraint) { return groupRelationsById(ids, associationAttribute, idAttribute, repository, constraint, null); }
@Nonnull public static <ID extends Serializable, T extends HasID<ID>, U> Map<ID, List<U>> groupRelationsById( @Nonnull final Collection<ID> ids, @Nonnull final SingularAttribute<? super U, T> associationAttribute, @Nonnull final SingularAttribute<? super T, ID> idAttribute, @Nonnull final JpaSpecificationExecutor<U> repository, @Nullable final Sort sortCriteria) { return groupRelationsById(ids, associationAttribute, idAttribute, repository, null, sortCriteria); }
@Nonnull public static <ID extends Serializable, T extends HasID<ID>, U> Map<ID, List<U>> groupRelationsById( @Nonnull final Collection<ID> ids, @Nonnull final SingularAttribute<? super U, T> associationAttribute, @Nonnull final SingularAttribute<? super T, ID> idAttribute, @Nonnull final JpaSpecificationExecutor<U> repository, @Nullable final Specification<U> constraint, @Nullable final Sort sortCriteria) { final Specification<U> inSpec = inIdCollection(associationAttribute, idAttribute, ids); final Function<U, ID> idFunction = Functions.idOf(CriteriaUtils.jpaProperty(associationAttribute)); return groupRelationsInternal(ids, inSpec, repository, idFunction, constraint, sortCriteria); }
public static <ID extends Serializable, T extends HasID<ID>, U> Function<T, List<U>> createInverseMappingFunction( @Nonnull final Collection<? extends T> objects, @Nonnull final SingularAttribute<? super U, T> singularAttribute, @Nonnull final JpaSpecificationExecutor<U> repository, final boolean letFunctionThrowExceptionOnEmptyResult) { return createInverseMappingFunction( objects, singularAttribute, repository, null, letFunctionThrowExceptionOnEmptyResult); }
public static <ID extends Serializable, T extends HasID<ID>, U> Function<T, List<U>> createInverseMappingFunction( @Nonnull final Collection<? extends T> objects, @Nonnull final SingularAttribute<? super U, T> singularAttribute, @Nonnull final JpaSpecificationExecutor<U> repository, @Nullable final Specification<U> constraint, final boolean letFunctionThrowExceptionOnEmptyResult) { Objects.requireNonNull(objects, "objects must not be null"); Objects.requireNonNull(singularAttribute, "singularAttribute must not be null"); Objects.requireNonNull(repository, "repository must not be null"); if (Iterables.isEmpty(objects)) { return input -> Collections.emptyList(); } final Specification<U> relationSpec = inCollection(singularAttribute, objects); final Specification<U> compoundSpec = constraint != null ? JpaSpecs.and(relationSpec, constraint) : relationSpec; final List<U> relatedObjects = repository.findAll(compoundSpec); if (relatedObjects.isEmpty()) { return input -> Collections.emptyList(); } final Map<ID, List<U>> index = F.groupByIdAfterTransform(relatedObjects, CriteriaUtils.jpaProperty(singularAttribute)); final Function<ID, List<U>> indexFunc = letFunctionThrowExceptionOnEmptyResult ? forMap(index) : forMap(index, null); return indexFunc.compose(F::getId); }
public static <ENTITY, REPO extends JpaRepository<ENTITY, ?> & JpaSpecificationExecutor> SpecificationBuilder<ENTITY> selectDistinctFrom(REPO repository) { SpecificationBuilder<ENTITY> builder = selectFrom(repository); builder.distinct(); return builder; }
@Nonnull public static <ID extends Serializable, T extends HasID<ID>, U> Map<ID, List<U>> groupRelationsById( @Nonnull final Collection<? extends T> objects, @Nonnull final SingularAttribute<? super U, T> singularAttribute, @Nonnull final JpaSpecificationExecutor<U> repository, @Nullable final Sort sortCriteria) { return groupRelationsById(objects, singularAttribute, repository, null, sortCriteria); }
/** * 根据 customSpecification,进行分页查询。 * <p> * findAll() 方法,会进行两次查询,先做 count 查询,之后是具体查询,所以 Page 中包含了总数和具体查询结果集 * * @param repository 查询器 * @param currentPageNo 当前页,实际对应 jqgrid 传递过来的 page 参数,jqgrid 规定起始页为 1 * @param pageSize 页面可显示行数 * @param sort * @param customSpecification 除了 jqgrid 传递过来的查询条件外,自己又附加查询条件,与 filters AND 关系的查询条件,specification 的构造符合 SearchFilter 写法,详见示例项目。 * customSpecification 中指定的属性名称应该是待查询的 entity 中的属性名称,并且用改 entity 的 repository 进行查询 * @return */ public static Page getPage(JpaSpecificationExecutor repository, int currentPageNo, int pageSize, Sort sort, Specification customSpecification) { //jpa 中起始页为 0,但传递过来的参数 currentPageNo 不能小于1 Assert.isTrue(currentPageNo >= 1, "currentPageNo 需要 >= 1 "); currentPageNo = currentPageNo - 1; return repository.findAll(customSpecification, new PageRequest(currentPageNo, pageSize, sort)); }