Java Code Examples for org.springframework.data.jpa.domain.Specifications#where()

The following examples show how to use org.springframework.data.jpa.domain.Specifications#where() . These examples are extracted from open source projects. 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
/**
 * @name 搜索资源
 * @param resourceType 资源类型
 * @param resourceName 资源名称
 * @param page 分页的页码
 * @param size 每页的数量
 * @return
 * @throws Exception
 */
public Object search(String resourceType,
                     String resourceName,
                     int page,
                     int size) throws Exception {
    addResourcesAuto();
    Sort sort = new Sort(Sort.Direction.DESC, "id");
    PageRequest pageRequest = new PageRequest(page, size, sort);
    Specifications<Resource> conditions = null;
    if (StringUtils.isNotBlank(resourceName)) {
        if (conditions == null)
            conditions = Specifications.where(SpecificationFactory.containsLike("resource_name", resourceName));
        else
            conditions = conditions.and(SpecificationFactory.containsLike("resource_name", resourceName));
    }
    if (StringUtils.isNotBlank(resourceType)) {
        if (conditions == null)
            conditions = Specifications.where(SpecificationFactory.containsLike("resource_type", resourceType));
        else
            conditions = conditions.and(SpecificationFactory.containsLike("resource_type", resourceType));
    }
    Page<Resource> page1 = null;
    if (conditions == null)
        page1 = resourceRepository.findAll(pageRequest);
    else
        page1 = resourceRepository.findAll(conditions, pageRequest);
    return page1;
}
 
Example 2
@Override
public long countByExample(final T entity, final SearchParameters sp) {
    Validate.notNull(entity, "The entity cannot be null");
    if (sp.hasNamedQuery()) {
        return getNamedQueryUtil().numberByNamedQuery(sp).intValue();
    }
    Specifications<T> spec = Specifications.where(byExampleSpecification.byExampleOnEntity(entity, sp));
    spec = RangeSpecification.andRangeIfSet(spec, sp.getRanges());
    spec = PropertySelectorSpecification.andPropertySelectorIfSet(spec, sp);
    return super.count(spec);
}
 
Example 3
@Override
public Page<T> findByExample(final T example, final List<Range<?, ?>> ranges, final Pageable pageable) {
    SearchParameters searchParameter = new SearchParameters();
    Specifications<T> spec = Specifications.where(byExampleSpecification.byExampleOnEntity(example,
            searchParameter));
    spec = RangeSpecification.andRangeIfSet(spec, ranges);
    spec = PropertySelectorSpecification.andPropertySelectorIfSet(spec, searchParameter);
    return findAll(spec, pageable);
}
 
Example 4
@Override
public Page<T> findByExample(final T example, final Pageable pageable) {
    SearchParameters searchParameter = new SearchParameters();
    Specifications<T> spec = Specifications.where(byExampleSpecification.byExampleOnEntity(example,
            searchParameter));
    spec = RangeSpecification.andRangeIfSet(spec, searchParameter.getRanges());
    spec = PropertySelectorSpecification.andPropertySelectorIfSet(spec, searchParameter);
    return findAll(spec, pageable);
}
 
Example 5
@Override
public List<T> findByExample(final T entity, final SearchParameters searchParameter) {
    Assert.notNull(searchParameter, "Search parameters required");
    if (searchParameter.hasNamedQuery()) {
        return getNamedQueryUtil().findByNamedQuery(searchParameter);
    }
    Specifications<T> spec = Specifications.where(byExampleSpecification.byExampleOnEntity(entity,
            searchParameter));
    spec = RangeSpecification.andRangeIfSet(spec, searchParameter.getRanges());
    spec = PropertySelectorSpecification.andPropertySelectorIfSet(spec, searchParameter);
    return findAll(spec);
}
 
Example 6
@Override
public Optional<T> findOneByExample(final T entity, final SearchParameters searchParameter) {
    Assert.notNull(searchParameter, "Search parameters required");
    Specifications<T> spec = Specifications.where(byExampleSpecification.byExampleOnEntity(entity,
            searchParameter));
    spec = RangeSpecification.andRangeIfSet(spec, searchParameter.getRanges());
    spec = PropertySelectorSpecification.andPropertySelectorIfSet(spec, searchParameter);
    return Optional.ofNullable(super.findOne(spec));
}
 
Example 7
public JpaDynamicSpecificationBuilder() {
    this.specification = Specifications.where(null);
}