admin管理员组文章数量:1406308
I have three specifications that, I would like to combine into one.
static Specification<Car> hasCarModel(List<String> modelsList) {
return (car, cq, cb) -> cb.and(car.get("model").in(modelsList));
}
static Specification<Car> hasCreatedDateGreaterThanOrEqualToStartDate(LocalDateTime _startDate) {
return (car, cq, cb) -> cb.greaterThanOrEqualTo(car.get("carCreatedDate"), _startDate);
}
static Specification<Car> hasCreatedDateLessThanOrEqualToEndDate(LocalDateTime _endDate) {
return (car, cq, cb) -> cb.lessThanOrEqualTo(car.get("carCreatedDate"), _endDate);
}
Is this doable? If so, how would you do it?
I have three specifications that, I would like to combine into one.
static Specification<Car> hasCarModel(List<String> modelsList) {
return (car, cq, cb) -> cb.and(car.get("model").in(modelsList));
}
static Specification<Car> hasCreatedDateGreaterThanOrEqualToStartDate(LocalDateTime _startDate) {
return (car, cq, cb) -> cb.greaterThanOrEqualTo(car.get("carCreatedDate"), _startDate);
}
static Specification<Car> hasCreatedDateLessThanOrEqualToEndDate(LocalDateTime _endDate) {
return (car, cq, cb) -> cb.lessThanOrEqualTo(car.get("carCreatedDate"), _endDate);
}
Is this doable? If so, how would you do it?
Share Improve this question edited Mar 9 at 4:15 Ferry To 1,5693 gold badges39 silver badges55 bronze badges asked Mar 5 at 21:27 Bondo KalomboBondo Kalombo 413 bronze badges1 Answer
Reset to default 0Yes, it is possible to combine the specifications into one. we have to use Specification
API in Spring Data to combine. You can combine the specifications using the and()
and or()
methods.
here's how yu can do it.
Specification<Car> combinedSpecification(List<String> modelsList, LocalDateTime startDate, LocalDateTime endDate)
{
return Specification.where(hasCarModel(modelsList))
.and(hasCreatedDateGreaterThanOrEqualToStartDate(startDate))
.and(hasCreatedDateLessThanOrEqualToEndDate(endDate));
}
本文标签: javaIs it possible to combine three Spring Data Specifications into oneStack Overflow
版权声明:本文标题:java - Is it possible to combine three Spring Data Specifications into one? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745006771a2637310.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论