如何修改org.springframework.data.jpa.domain.Specification对象?

时间:2018-03-15 10:02:53

标签: java spring spring-boot spring-data spring-data-jpa

我已经通过@RestController实现了Specification in来设置Specificaton对象中的值。

public ResponseEntity<ServiceResponse> searchUnexpectedLikelyToChurn(@RequestHeader HttpHeaders headers,
            @PageableDefault(page = 0, size = 10, sort = "buyer", direction = Direction.ASC) Pageable pageable, 
            @Conjunction(value = {
                    @Or({           
                        @Spec(path="buyer", params="search", spec=LikeIgnoreCase.class),
                        @Spec(path = "email", params = "search", spec = LikeIgnoreCase.class),
                        @Spec(path = "shipCity", params = "search", spec = LikeIgnoreCase.class),
                        @Spec(path = "shipState", params = "search", spec = LikeIgnoreCase.class),
                        @Spec(path = "country", params = "search", spec = LikeIgnoreCase.class)
                    })}, and = @Spec(path = "company",params = "company", spec = Equal.class)) Specification<CustomerEntity> customerSpec   

我已经使用了这个库[https://github.com/tkaczmarzyk/specification-arg-resolver#enabling-spec-annotations-in-your-spring-app]

现在我需要在这个Specification对象上添加另一个条件,它是一个java.util.Date对象,它是根据代码而不是用户输入计算的。我该如何添加?

我目前的DAO实施是:

 @Repository
public interface CustomerDao extends JpaRepository<CustomerEntity, Integer>, JpaSpecificationExecutor<CustomerEntity> { 
}

1 个答案:

答案 0 :(得分:0)

您可以如下使用org.springframework.data.jpa.domain.Specification界面:

Specification combinedSpec - Specification.where(customerSpec)
                                          .and(calculateAdditionalSpec());

如果您使用旧版的Spring Data JPA,请使用Specifications帮助程序类。