Specification Criteria - Spring Boot Part 1

Specification<> is immutable, and() returns a new object combining the previous ones.
so we need to re-assign.
i found that when i added the inCategory() in the ProductSpec class.

public void fetchProductsBySpecs(String name, BigDecimal minPrice, BigDecimal maxPrice, Category category){

    Specification<Product> spec = Specification.where(null);

    spec = Optional.ofNullable(name)
            .map(ProductSpec::hasName)
            .map(spec::and)
            .orElse(spec);

    spec = Optional.ofNullable(minPrice)
            .map(ProductSpec::hasPriceGreaterThanOrEqualTo)
            .map(spec::and)
            .orElse(spec);

    spec = Optional.ofNullable(maxPrice)
            .map(ProductSpec::hasPriceLessThanOrEqualTo)
            .map(spec::and)
            .orElse(spec);

    spec = Optional.ofNullable(category)
            .map(ProductSpec::inCategory)
            .map(spec::and)
            .orElse(spec);

    List<Product> products = productRepository.findAll(spec);
    products.forEach(System.out::println);
}

public static Specification<Product> inCategory(Category category){
    return (root, query, criteriaBuilder) ->
            criteriaBuilder.like(root.get("category").get("name"), "%" + category.getName() + "%");
}

this is how it works with me. am i right?