Spring Data Jpa - 类型规范<T>已弃用 [英] Spring Data Jpa - The type Specifications<T> is deprecated

查看:20
本文介绍了Spring Data Jpa - 类型规范<T>已弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从链接实现逻辑:

解决方案

你的 repo 代码需要像这样扩展 JpaSpecificationExecutor:

public interface EmployeeRepository extends JpaRepository,JpaSpecificationExecutor<员工>{}

JpaSpecicationExecutor 有那些可以调用的方法:

公共接口 JpaSpecificationExecutor;{可选的 TfindOne(@Nullable Specification<T> var1);列表<T>findAll(@Nullable Specification<T> var1);页面<T>findAll(@Nullable Specification<T> var1, Pageable var2);列表<T>findAll(@Nullable Specification<T> var1, Sort var2);long count(@Nullable Specification<T> var1);}

然后你可以这样做:

public void findAllCustomersByFirstName(String firstName) {员工资源库.findAll(EmployeeSpecification.textInAllColumns(firstName));}

我将您的规范更改为使用 lambda:

public class EmployeeSpecification {公共静态规范<员工>textInAllColumns(字符串文本){if (!text.contains("%")) {文本 = "%" + 文本 + "%";}final String finalText = text;return (Specification) (root, query, builder) ->builder.or(root.getModel().getDeclaredSingularAttributes().stream().filter(a -> {if (a.getJavaType().getSimpleName().equalsIgnoreCase("string")) {返回真;}其他{返回假;}}).map(a -> builder.like(root.get(a.getName()), finalText)).toArray(Predicate[]::new));}}

您可以在此处查看答案中代码的更新版本:https://github.com/zifnab87/spring-boot-rest-api-helpers/blob/26501c1d6afcd6afa8ea43c121898db85b4e5esrc/main/java/springboot/rest/specifications/CustomSpecifications.java#L172

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

解决方案

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

这篇关于Spring Data Jpa - 类型规范&lt;T&gt;已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆