I have a RequestMaping that get a Search json class as body params. I want to create proper Specification from this search json object so that pass to my Repository like this:
pagesResult = myRepository.findAll(mySpec)
I have problme with parsing and Dynamically add items to specification. I want to achieve something like this:
@Override
public Phones searchPhones(int pageNumber, Map<String, String> searchObject) {
List<PhoneSpecification> specificationslist = new ArrayList<>();
generateSpecifications(searchObject, specificationslist); //fill specificationList
Specification<Phone> specificationOfPhone;
for (PhoneSpecification spec :
specificationslist) {
//this is my problem , I had to dynamically increase my Specification like this:
specificationOfPhone = specificationOfPhone + Specifications.and(spec);
}
mobileRepository.findAll(specificationOfPhone);
I finally achieve this by adding where to first specification and handle all by adding like this:
You can change your code to this:
I assumed that
PhoneSpecification
is extending/implementsSpecification<T>
.Specifications.where(null);
will return empty specification which can be chained with others.Because
Specifications<T>
extendsSpecification<T>
you can use it with yourfindAll
method.