title: Java 8 - The method save(S) in the type CrudRepository<Developer,Long> is not applicable for the arguments (Optional<Developer>)
body: <p>I'm starting into java development using spring framework, and for the purpose of getting something a little more complex than a hello world I found this tutorial and tried to follow.</p>
  
  <p><a href="https://www.toptal.com/spring/beginners-guide-to-mvc-with-spring-framework" rel="nofollow noreferrer">https://www.toptal.com/spring/beginners-guide-to-mvc-with-spring-framework</a></p>
  
  <p>The issue i'm finding is that, the proposed code: Inside the DevelopersController.java is getting errors in eclipse due to the fact that my my classes that extends "CrudRepository" return a Optional &lt; Skill> and Optional &lt; Developer> instead of just a Skill/Developer object.</p>
  
  <pre><code>    @RequestMapping(value="/developer/{id}/skills", method=RequestMethod.POST)
      public String developersAddSkill(@PathVariable Long id, @RequestParam Long skillId, Model model) {
      Skill skill = skillRepository.findOne(skillId);
      Developer developer = repository.findOne(id);
  
      if (developer != null) {
          if (!developer.hasSkill(skill)) {
              developer.getSkills().add(skill);
          }
          repository.save(developer);
          model.addAttribute("developer", repository.findOne(id));
          model.addAttribute("skills", skillRepository.findAll());
          return "redirect:/developer/" + developer.getId();
      }
  
      model.addAttribute("developers", repository.findAll());
      return "redirect:/developers";
  }
  </code></pre>
  
  <p>I looked for some information on Java 8 Optional but since I still lack coding experience I having a hard time undestanding how to use it properly.</p>
  
  <p>I changed the code to and managed to get one of the errors out....</p>
  
  <pre><code>    @RequestMapping(value="/developer/{id}/skills", method=RequestMethod.POST)
      public String developersAddSkill(
          @PathVariable Long id, 
          @RequestParam Long skillId, 
          Model model) {
      Optional&lt;Skill&gt; skill = skillRepository.findById(skillId);
      Optional&lt;Developer&gt; developer = repository.findById(id);
  
      developer.get().getSkills();
      if (developer != null) {
          if (!developer.get().hasSkill(skill)) {
              developer.get().getSkills().add(skill);
          }
          repository.save(developer);
          model.addAttribute("developer", repository.findById(id));
          model.addAttribute("skills", skillRepository.findAll());
          return "redirect:/developer/" + developer.getId();
      }
      return "Confused";
  }
  </code></pre>
  
  <p>But i'm still getting the errors bellow in my eclipse:</p>
  
  <ul>
  <li>The method add(Skill) in the type List is not applicable for the arguments (Optional)</li>
  <li>The method getId() is undefined for the type Optional</li>
  <li>The method save(S) in the type CrudRepository is not applicable for the arguments (Optional)</li>
  </ul>
  
  <p>How can i fix this?
  Also Why does the line:</p>
  
  <pre><code>if (!developer.get().hasSkill(skill)) {
  </code></pre>
  
  <p>shows no errors, but the line:</p>
  
  <pre><code>developer.get().getSkills().add(skill);
  </code></pre>
  
  <p>does?</p>