Example usage for org.springframework.data.jpa.repository JpaSpecificationExecutor findAll

List of usage examples for org.springframework.data.jpa.repository JpaSpecificationExecutor findAll

Introduction

In this page you can find the example usage for org.springframework.data.jpa.repository JpaSpecificationExecutor findAll.

Prototype

List<T> findAll(@Nullable Specification<T> spec, Sort sort);

Source Link

Document

Returns all entities matching the given Specification and Sort .

Usage

From source file:org.pushio.webapp.support.persistence.SearchFilter.java

/**
 * @see Page<BJob> jobs = SearchFilter.query("jName[like]=sma&jId=3&BEmployees.employeeId=2", pageable, BJob.class, jobsRepository);
 * @param query  -->  "jName[like]=sma&jId=3&BEmployees.employeeId=2"
 * @param pageable --> PageRequest pageable;
 * @param entity --> BJob.class/* ww  w  .  j ava  2s .  co  m*/
 * @param repository --> jobsRepository JpaSpecificationExecutor
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Page query(String query, PageRequest pageable, Class<?> entity,
        JpaSpecificationExecutor repository) {
    String strs[] = StringUtils.split(query, '&');
    if (strs == null || strs.length == 0) {
        throw new IllegalArgumentException("query ?null");
    } else {
        Map<String, Object> searchParams = new HashMap<String, Object>(strs.length); // TreeMap??
        for (String str : strs) {
            String keyValue[] = StringUtils.split(str, '=');
            if (keyValue.length != 2) {
                throw new IllegalArgumentException("\"" + query + "\"??");
            }
            searchParams.put(StringUtils.trim(keyValue[0]), keyValue[1]);
        }
        Specification spec = DynamicSpecifications
                .bySearchFilter(SearchFilter.parse2Filter(searchParams).values(), entity);
        return repository.findAll(spec, pageable);
    }
}