[英]Spring Data Jpa - The type Specifications<T> is deprecated
I am implementation the logic from link: Spring Data - Multi-column searches where I am looking to search by FirstName
.我正在实现链接中的逻辑: Spring Data - Multi-column search where I want to search by
FirstName
。
As per link: https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/domain/Specifications.html根据链接: https : //docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/domain/Specifications.html
EmployeeSpecification.java员工规范.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 EmployeeRepository.java
public interface EmployeeRepository extends JpaRepository<Employee, Long>{
List<Employee> findAll(Specification<Employee> spec);
}
EmployeeServiceImpl.java 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
此行的多个标记 - 类型规范中的方法 where(Specification) 不适用于参数 (Specification) - 不推荐使用类型规范
Your repo code needs to extend JpaSpecificationExecutor
like that:您的 repo 代码需要像这样扩展
JpaSpecificationExecutor
:
public interface EmployeeRepository extends JpaRepository<Employee, Long>,
JpaSpecificationExecutor<Employee> {
}
JpaSpeficationExecutor has those methods that can be called: JpaSpecicationExecutor 具有可以调用的方法:
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:我将您的规范更改为使用 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您可以在此处查看答案中的代码的更新版本: https : //github.com/zifnab87/spring-boot-rest-api-helpers/blob/26501c1d6afcd6afa8ea43c121898db85b4e5dbe/src/main/java/springboot /rest/specifications/CustomSpecifications.java#L172
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.