org.springframework.data.jpa.repository.JpaSpecificationExecutor Java Examples

The following examples show how to use org.springframework.data.jpa.repository.JpaSpecificationExecutor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CommonQueryService.java    From WeBASE-Collect-Bee with Apache License 2.0 6 votes vote down vote up
public <T> CommonResponse getPageListByCommonReq(CommonBiParaQueryPageReq<String> req,
        JpaSpecificationExecutor<T> repository) {
    PageRequest pr = (PageRequest) req.convert();
    try {
        if (StringUtils.isEmpty(req.getReqParaValue1()) || StringUtils.isEmpty(req.getReqParaName1())) {
            return ResponseUtils.paramError("para1 should not be empty");
        }
        if (StringUtils.isEmpty(req.getReqParaValue2()) || StringUtils.isEmpty(req.getReqParaName2())) {
            CommonParaQueryPageReq<String> uniReq = new CommonParaQueryPageReq<>();
            BeanUtil.copyProperties(req, uniReq);
            uniReq.setReqParaName(req.getReqParaName1()).setReqParaValue(req.getReqParaValue1());
            return getPageListByCommonReq(uniReq, repository);
        }
        Specification<T> spec = CommonReqParaSpecification.queryByCriteriaEqual(req);
        Page<T> page = repository.findAll(spec, pr);
        CommonPageRes<T> ret = new CommonPageRes<>(req);
        ret.setResult(page.getContent()).setTotalCount(page.getTotalElements()).setPageNo(req.getPageNo())
                .setPageSize(req.getPageSize());
        return ResponseUtils.data(ret);
    } catch (DateException e) {
        return ResponseUtils.paramError("invalid date format " + e.getMessage());
    }
}
 
Example #2
Source File: TimeRangeQueryService.java    From WeBASE-Collect-Bee with Apache License 2.0 6 votes vote down vote up
/**
 * page query by a time range.
 * 
 * @param req
 * @param repository
 * @return
 */
public <T> CommonResponse getPageListByTimeRange(TimeRangeQueryReq req, JpaSpecificationExecutor<T> repository) {
    CommonTimeCondition condition = new CommonTimeCondition();
    try {
        condition.setBeginTime(DateUtil.parse(req.getBeginTime())).setEndTime(DateUtil.parse(req.getEndTime()));
    } catch (DateException e) {
        log.error("DateUtil convert error: {}", e.getMessage());
        return ResponseUtils.paramError("invalid time format, " + e.getMessage());
    }
    int pageNo = req.getPageNo();
    int pageSize = req.getPageSize();
    PageRequest pr = (PageRequest) req.convert();
    Specification<T> spec = TimeSpecification.queryByCriteria(condition);
    Page<T> page = repository.findAll(spec, pr);
    CommonPageRes<T> ret = new CommonPageRes<>(req);
    ret.setResult(page.getContent()).setTotalCount(page.getTotalElements()).setPageNo(req.getPageNo())
            .setPageSize(req.getPageSize());
    return ResponseUtils.data(ret);
}
 
Example #3
Source File: Datatable.java    From kafka-webview with MIT License 6 votes vote down vote up
/**
 * Constructor.  See Builder instance.
 * @param repository Underlying JPA repository instance.
 * @param pageable Pageable instance.
 * @param requestParams All request parameters available.
 * @param url URL the datatable lives at.
 * @param label Display label for the datatable.
 * @param columns One or more columns in the datatable.
 * @param filters Zero or more filters for the table.
 * @param datatableSearch Zero or one search.
 * @param noRecordsFoundTemplatePath What template to load if no records are found.
 */
public Datatable(
    final JpaSpecificationExecutor<T> repository,
    final Pageable pageable,
    final Map<String, String> requestParams,
    final String url,
    final String label,
    final List<DatatableColumn> columns,
    final List<DatatableFilter> filters,
    final DatatableSearch datatableSearch,
    final String noRecordsFoundTemplatePath
) {
    this.repository = Objects.requireNonNull(repository);
    this.pageable = Objects.requireNonNull(pageable);
    this.requestParams = Collections.unmodifiableMap(new HashMap<>(requestParams));
    this.url = url;
    this.label = label;
    this.columns = columns;
    this.filters = filters;
    this.datatableSearch = datatableSearch;
    this.noRecordsFoundTemplatePath = Objects.requireNonNull(noRecordsFoundTemplatePath);
}
 
Example #4
Source File: UnitBasicQueryService.java    From WeBASE-Collect-Bee with Apache License 2.0 5 votes vote down vote up
/**
 * Page query by parameter name and parameter value.
 * 
 * @param UnitParaQueryPageReq
 * @param unitType
 * @return
 */
public CommonResponse getPageListByReq(UnitParaQueryPageReq<String> req, String unitType) {
    String repositoryName = StringUtils.uncapitalize(req.getUnitName() + unitType);
    if (repositoryService.getJpaSpecificationExecutor(repositoryName).isPresent()) {
        JpaSpecificationExecutor j = repositoryService.getJpaSpecificationExecutor(repositoryName).get();
        return commonQueryService.getPageListByCommonReq(req, j);
    } else {
        return ResponseUtils.paramError("The unit name is invalid: " + req.getUnitName());
    }
}
 
Example #5
Source File: UnitBasicQueryService.java    From WeBASE-Collect-Bee with Apache License 2.0 5 votes vote down vote up
public CommonResponse getPageListByReq(UnitBiParaQueryPageReq<String> req, String unitType) {
    String repositoryName = StringUtils.uncapitalize(req.getUnitName() + unitType);
    if (repositoryService.getJpaSpecificationExecutor(repositoryName).isPresent()) {
        JpaSpecificationExecutor j = repositoryService.getJpaSpecificationExecutor(repositoryName).get();
        return commonQueryService.getPageListByCommonReq(req, j);
    } else {
        return ResponseUtils.paramError("The unit name is invalid: " + req.getUnitName());
    }
}
 
Example #6
Source File: UnitBasicQueryService.java    From WeBASE-Collect-Bee with Apache License 2.0 5 votes vote down vote up
public CommonResponse getPageListByReq(UnitSpecificationQueryPageReq req, String unitType) {
    String repositoryName = StringUtils.uncapitalize(req.getUnitName() + unitType);
    if (repositoryService.getJpaSpecificationExecutor(repositoryName).isPresent()) {
        JpaSpecificationExecutor j = repositoryService.getJpaSpecificationExecutor(repositoryName).get();
        return commonQueryService.getPageListByCommonReq(req, j);
    } else {
        return ResponseUtils.paramError("The unit name is invalid: " + req.getUnitName());
    }
}
 
Example #7
Source File: UnitBasicQueryService.java    From WeBASE-Collect-Bee with Apache License 2.0 5 votes vote down vote up
/**
 * Page query by time range.
 * 
 * @param UnitTimeRangeQueryPageReq
 * @param unitType
 * @return
 */
public CommonResponse getPageListByReq(UnitTimeRangeQueryPageReq req, String unitType) {
    String repositoryName = StringUtils.uncapitalize(req.getUnitName() + unitType);
    if (repositoryService.getJpaSpecificationExecutor(repositoryName).isPresent()) {
        JpaSpecificationExecutor j = repositoryService.getJpaSpecificationExecutor(repositoryName).get();
        return timeRangeQueryService.getPageListByTimeRange(req, j);
    } else {
        return ResponseUtils.paramError("The unit name is invalid: " + req.getUnitName());
    }
}
 
Example #8
Source File: CommonQueryService.java    From WeBASE-Collect-Bee with Apache License 2.0 5 votes vote down vote up
/**
 * get one page of object list by param type and value.
 * 
 * @param req: CommonParaQueryPageReq contains order, orderBy, pageNo, pageSize, paramType, paramValue
 * @param repository
 * @return: CommonResponse
 */
public <T> CommonResponse getPageListByCommonReq(CommonParaQueryPageReq<String> req,
        JpaSpecificationExecutor<T> repository) {
    try {
        PageRequest pr = (PageRequest) req.convert();
        Specification<T> spec = CommonReqParaSpecification.queryByCriteriaEqual(req);
        Page<T> page = repository.findAll(spec, pr);
        CommonPageRes<T> ret = new CommonPageRes<>(req);
        ret.setResult(page.getContent()).setTotalCount(page.getTotalElements()).setPageNo(req.getPageNo())
                .setPageSize(req.getPageSize());
        return ResponseUtils.data(ret);
    } catch (DateException e) {
        return ResponseUtils.paramError("invalid date format " + e.getMessage());
    }
}
 
Example #9
Source File: CommonQueryService.java    From WeBASE-Collect-Bee with Apache License 2.0 5 votes vote down vote up
public <T> CommonResponse getPageListByCommonReq(CommonSpecificationQueryPageReq req,
        JpaSpecificationExecutor<T> repository) {
    PageRequest pr = (PageRequest) req.convert();
    try {
        Specification<T> spec = CommonReqParaSpecification.queryByCriteriaEqual(req);
        Page<T> page = repository.findAll(spec, pr);
        CommonPageRes<T> ret = new CommonPageRes<>(req);
        ret.setResult(page.getContent()).setTotalCount(page.getTotalElements()).setPageNo(req.getPageNo())
                .setPageSize(req.getPageSize());
        return ResponseUtils.data(ret);
    } catch (DateException e) {
        return ResponseUtils.paramError("invalid date format " + e.getMessage());
    }
}
 
Example #10
Source File: RepositoryService.java    From WeBASE-Collect-Bee with Apache License 2.0 5 votes vote down vote up
public Optional<JpaSpecificationExecutor> getJpaSpecificationExecutor(String name) {
    for (String k : specifications.keySet()) {
        if (name.equalsIgnoreCase(k)) {
            return Optional.of(specifications.get(k));
        }
    }
    return Optional.empty();
}
 
Example #11
Source File: SpecificationBuilder.java    From spring-repository-plus with Apache License 2.0 5 votes vote down vote up
public static <T, R extends JpaRepository<T, ?> & JpaSpecificationExecutor>
SpecificationBuilder<T> selectFrom(R repository) {
    SpecificationBuilder<T> builder = new SpecificationBuilder<>();
    builder.repository = repository;
    builder.specification = new SpecificationImpl();
    return builder;
}
 
Example #12
Source File: AbstractRepository.java    From pnc with Apache License 2.0 4 votes vote down vote up
public AbstractRepository(
        JpaRepository<T, ID> springRepository,
        JpaSpecificationExecutor<T> springSpecificationsExecutor) {
    this.springRepository = springRepository;
    this.springSpecificationsExecutor = springSpecificationsExecutor;
}
 
Example #13
Source File: SpecificationBuilder.java    From spring-repository-plus with Apache License 2.0 4 votes vote down vote up
public static <T, R extends JpaRepository<T, ?> & JpaSpecificationExecutor>
SpecificationBuilder<T> selectDistinctFrom(R repository) {
    SpecificationBuilder<T> builder = selectFrom(repository);
    builder.distinct();
    return builder;
}
 
Example #14
Source File: Datatable.java    From kafka-webview with MIT License 4 votes vote down vote up
public Builder<T> withRepository(final JpaSpecificationExecutor<T> repository) {
    this.repository = repository;
    return this;
}
 
Example #15
Source File: JpaUtils.java    From spring-boot with Apache License 2.0 3 votes vote down vote up
/**
 * 根据 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));
}
 
Example #16
Source File: RSQLJPASupport.java    From rsql-jpa-specification with MIT License 2 votes vote down vote up
/**
 * Returns the number of instances that the given {@link Specification} will return.
 *
 * @param jpaSpecificationExecutor JPA repository
 * @param rsqlQuery the {@link Specification} to count instances for. Can be {@literal null}.
 * @return the number of instances.
 */
public static long count(JpaSpecificationExecutor<?> jpaSpecificationExecutor, @Nullable String rsqlQuery) {
	return jpaSpecificationExecutor.count(toSpecification(rsqlQuery));
}
 
Example #17
Source File: RSQLJPASupport.java    From rsql-jpa-specification with MIT License 2 votes vote down vote up
/**
 * Returns all entities matching the given {@link Specification} and {@link Sort}.
 *
 * @param jpaSpecificationExecutor JPA repository
 * @param rsqlQuery can be {@literal null}.
 * @param sort can be {@literal null}, comma delimited.
 * @return never {@literal null}.
 */
public static List<?> findAll(JpaSpecificationExecutor<?> jpaSpecificationExecutor, @Nullable String rsqlQuery, @Nullable String sort) {
	return StringUtils.hasText(sort)
			? jpaSpecificationExecutor.findAll(toSpecification(rsqlQuery), Sort.by(Direction.ASC, StringUtils.commaDelimitedListToStringArray(sort)))
			: jpaSpecificationExecutor.findAll(toSpecification(rsqlQuery));
}
 
Example #18
Source File: RSQLJPASupport.java    From rsql-jpa-specification with MIT License 2 votes vote down vote up
/**
 * Returns all entities matching the given {@link Specification} and {@link Sort}.
 *
 * @param jpaSpecificationExecutor JPA repository
 * @param rsqlQuery can be {@literal null}.
 * @param sort must not be {@literal null}.
 * @return never {@literal null}.
 */
public static List<?> findAll(JpaSpecificationExecutor<?> jpaSpecificationExecutor, @Nullable String rsqlQuery, Sort sort) {
	return jpaSpecificationExecutor.findAll(toSpecification(rsqlQuery), sort);
}
 
Example #19
Source File: RSQLJPASupport.java    From rsql-jpa-specification with MIT License 2 votes vote down vote up
/**
 * Returns a {@link Page} of entities matching the given {@link Specification}.
 *
 * @param jpaSpecificationExecutor JPA repository
 * @param rsqlQuery can be {@literal null}.
 * @param pageable must not be {@literal null}.
 * @return never {@literal null}.
 */
public static Page<?> findAll(JpaSpecificationExecutor<?> jpaSpecificationExecutor, @Nullable String rsqlQuery, Pageable pageable) {
	return jpaSpecificationExecutor.findAll(toSpecification(rsqlQuery), pageable);
}
 
Example #20
Source File: RSQLJPASupport.java    From rsql-jpa-specification with MIT License 2 votes vote down vote up
/**
 * Returns all entities matching the given {@link Specification}.
 *
 * @param jpaSpecificationExecutor JPA repository
 * @param rsqlQuery can be {@literal null}.
 * @return never {@literal null}.
 */
public static List<?> findAll(JpaSpecificationExecutor<?> jpaSpecificationExecutor, @Nullable String rsqlQuery) {
	return jpaSpecificationExecutor.findAll(toSpecification(rsqlQuery));
}
 
Example #21
Source File: RSQLJPASupport.java    From rsql-jpa-specification with MIT License 2 votes vote down vote up
/**
 * Returns a single entity matching the given {@link Specification} or {@link Optional#empty()} if none found.
 *
 * @param jpaSpecificationExecutor JPA repository
 * @param rsqlQuery can be {@literal null}.
 * @return never {@literal null}.
 * @throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one entity found.
 */
public static Optional<?> findOne(JpaSpecificationExecutor<?> jpaSpecificationExecutor, @Nullable String rsqlQuery) {
	return jpaSpecificationExecutor.findOne(toSpecification(rsqlQuery));
}