【问题标题】:Spring Data Jpa - The type Specifications<T> is deprecatedSpring Data Jpa - 类型 Specifications<T> 已弃用
【发布时间】:2019-11-28 10:33:55
【问题描述】:

我正在执行链接中的逻辑:Spring Data - Multi-column searches 我希望通过FirstName 进行搜索。

根据链接: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)));
    }
}

错误:

此行有多个标记 - 类型 Specifications 中的方法 where(Specification) 不适用于参数 (规格) - 类型规格已弃用

【问题讨论】:

    标签: java spring-data-jpa specifications jpa-2.2


    【解决方案1】:

    您的回购代码需要像这样扩展JpaSpecificationExecutor

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

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

    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);
    }
    

    那么你可以这样做:

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

    我更改了您的规范以使用 lambda:

    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));
        }
    }
    

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

    【讨论】:

    • 谢谢。我们真的需要输入种姓(Specification&lt;Employee&gt;)吗?另外,我们能不能“用一个 return 语句替换这个 if-then-else 语句。”
    • 如何在搜索中包含多列数据库(或参数)?
    • 我想我已经重构了我链接的 repo 中的代码。你还可以看到,我在示例中使用了一个列列表来进行搜索
    • @Michali - 同意,我已经看过了,你的代码涉及很多东西。你会帮助创建一个简化的日期(不存在于你的代码中),整数和字符串吗?
    • @PAA 这是基于这个问题的题外话 - 我可以在上一个问题中添加一些更相关的代码
    猜你喜欢
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 2021-04-05
    • 2015-09-18
    • 1970-01-01
    • 2014-02-03
    • 2012-12-25
    • 2013-10-12
    相关资源
    最近更新 更多