'Save Method not workin with the repository

I have a method in my Service Class

public Optional<Product> autenticarUsuario(Optional<Product> product) {

        Optional<Product> status = repository.findById(product.get().getId());
        
        if(status.isPresent()) {
            if(product.get().getType() == 1) {
                return Optional.of(repository.save(product));
            
            } else {
                return Optional.empty();
                
            }

Where I need to return an ok to save if the string is "1", but return nothing if its not. This is my repository

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
    public List<Product> findAllByNameContainingIgnoreCase(String name);
    public Optional<Product> findById(long id);
    
}

The error that the program gives me is: "The method save(S) in the type CrudRepository<Product,Long> is not applicable for the arguments (Optional)"



Solution 1:[1]

I think you need to pass an argument of type Product instead of Optional. repository.save(product.get()) also as Saad Zimat points out you should include a product.isPresent() check in the if statement to ensure there is an entity to save.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1