我已经通过@RestController实现了Specification来设置Specicaton对象中的值。
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> {
}
最佳答案
您可以使用org.springframework.data.jpa.domain.Specification
接口(interface),如下所示:
Specification combinedSpec - Specification.where(customerSpec)
.and(calculateAdditionalSpec());
如果您使用旧版本的 Spring Data JPA,请使用 Specifications
辅助类。
关于java - 如何修改 org.springframework.data.jpa.domain.Specification 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49296462/