2

I am implementation the logic from link: Spring Data - Multi-column searches where I am looking to search by FirstName.

As per link: https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/domain/Specifications.html

EmployeeSpecification.java

public class EmployeeSpecification {
    public static Specification<Employee> textInAllColumns(String text) {
        if (!text.contains("%")) {
            text = "%" + text + "%";
        }
        final String finalText = text;

        return new Specification<Employee>() {
            @Override
            public Predicate toPredicate(Root<Employee> root, CriteriaQuery<Employee> cq, CriteriaBuilder builder) {
                return builder.or(root.getModel().getDeclaredSingularAttributes().stream().filter(a -> {
                    if (a.getJavaType().getSimpleName().equalsIgnoreCase("string")) {
                        return true;
                    } else {
                        return false;
                    }
                }).map(a -> builder.like(root.get(a.getName()), finalText)).toArray(Predicate[]::new));
            }
        };
    }
}

EmployeeRepository.java

public interface EmployeeRepository extends JpaRepository<Employee, Long>{
    List<Employee> findAll(Specification<Employee> spec);
}

EmployeeServiceImpl.java

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeRepository employeeRepository;

    @Override
    public void findAllCustomersByFirstName(String firstName) {
        employeeRepository.findAll(Specifications.where(EmployeeSpecification.textInAllColumns(firstName)));
    }
}

Error:

Multiple markers at this line - The method where(Specification) in the type Specifications is not applicable for the arguments (Specification) - The type Specifications is deprecated

enter image description here

Pra_A
  • 7,134
  • 12
  • 89
  • 170

1 Answers1

3

Your repo code needs to extend JpaSpecificationExecutor like that:

public interface EmployeeRepository extends JpaRepository<Employee, Long>, 
    JpaSpecificationExecutor<Employee> {
}

JpaSpeficationExecutor has those methods that can be called:

public interface JpaSpecificationExecutor<T> {
    Optional<T> findOne(@Nullable Specification<T> var1);

    List<T> findAll(@Nullable Specification<T> var1);

    Page<T> findAll(@Nullable Specification<T> var1, Pageable var2);

    List<T> findAll(@Nullable Specification<T> var1, Sort var2);

    long count(@Nullable Specification<T> var1);
}

Then you can do:

public void findAllCustomersByFirstName(String firstName) {
    employeeRepository.findAll(
            EmployeeSpecification.textInAllColumns(firstName)
    );
}

I changed your Specifications to use lambdas:

public class EmployeeSpecification {
    public static Specification<Employee> textInAllColumns(String text) {
        if (!text.contains("%")) {
            text = "%" + text + "%";
        }
        final String finalText = text;

        return  (Specification<Employee>) (root, query, builder) -> 
                builder.or(root.getModel().getDeclaredSingularAttributes().stream().filter(a -> {
                if (a.getJavaType().getSimpleName().equalsIgnoreCase("string")) {
                    return true;
                } else {
                    return false;
                }
            }).map(a -> builder.like(root.get(a.getName()), finalText)).toArray(Predicate[]::new));
    }
}

You can have a look here for the updated version of the code you have in your answer: https://github.com/zifnab87/spring-boot-rest-api-helpers/blob/26501c1d6afcd6afa8ea43c121898db85b4e5dbe/src/main/java/springboot/rest/specifications/CustomSpecifications.java#L172

Michail Michailidis
  • 10,050
  • 6
  • 44
  • 85
  • Thanks. Do we need really type caste `(Specification)`? Also, can we do "Replace this if-then-else statement by a single return statement." – Pra_A Jul 21 '19 at 06:21
  • 1
    How to include multiple columns of DB (or parameters) in search ? – Pra_A Jul 21 '19 at 14:03
  • I think I have refactored the code in the repo I have linked.. you can see also that I take a list of columns to do the search in the example – Michail Michailidis Jul 21 '19 at 20:23
  • @Michali - Agree, I already went through it, there are so many things involved in your code. Will you help to create simplified one with Date (not present in your code), Integer and String ? – Pra_A Jul 22 '19 at 04:59
  • @PAA this is offtopic based on this question - I can add some code in the previous question which is more related – Michail Michailidis Jul 22 '19 at 17:04
  • @PAA https://stackoverflow.com/a/46302324/986160 check this at the bottom – Michail Michailidis Jul 22 '19 at 17:09