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