title: Spring boot + Thymeleaf - Object with id null when editing
body: <p>When you select an Expense object from the data table for editing, the edit method in the controller sends the Expense object to the edit screen that is the same to insert a new record. When the object is sent to the save method, it has nullo code attribute where an insert occurs instead of an update. I do not understand why.</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>@Controller
  @RequestMapping("/despesas")
  public class DespesaController {
  
      @Autowired private DespesaService despesaService;
      @Autowired private DespesaRepository despesaRepository;
  
      @GetMapping("/add")
      public ModelAndView novo(Despesa despesa) {
          ModelAndView model = new ModelAndView("page/cadastro/despesa/cadDespesa");
          model.addObject("tiposDespesa", TipoDespesa.values());
          model.addObject("formasPagamento", FormaPagamento.values());
          model.addObject(despesa);
          return model;
      }
  
      @PostMapping("/save")
      public ModelAndView salvar(Despesa despesa, BindingResult result, RedirectAttributes attributes) {
          if (result.hasErrors()) {
              return novo(despesa);
          }
          despesa.setDataDespesa(new Date());
          despesaService.salvarDespesa(despesa);
          attributes.addFlashAttribute("mensagem", "Despesa Salva com Sucesso!");
          return new ModelAndView("redirect:/cadastroDespesa");
      }
  
      @GetMapping("/listDespesa")
      public ModelAndView listagemDeDespesas() {
          ModelAndView model = new ModelAndView("page/cadastro/despesa/listDespesa");
          model.addObject("despesas", despesaRepository.findAll());
          return model;
      }
  
      @GetMapping("/edit{id}")
      public ModelAndView editar(@PathVariable("id") Long codigo) {
          return novo(despesaRepository.findOne(codigo));
      }
  }
  </code></pre>
  
  <p><strong>FormEdit</strong></p>
  
  <pre><code>&lt;form th:object="${despesa}" method="POST" th:action="@{/despesas/save}"&gt;
   &lt;div class="box-body"&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-md-6"&gt;
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Data&lt;/label&gt;
                  &lt;div class="input-group"&gt;
                      &lt;div class="input-group-addon"&gt;
                          &lt;i class="fa fa-calendar"&gt;&lt;/i&gt;
                      &lt;/div&gt;
                      &lt;input type="text" th:field="*{dataDespesa}" class="form-control" disabled="disabled"&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Valor&lt;/label&gt;
                  &lt;div class="input-group"&gt;
                      &lt;span class="input-group-addon"&gt;&lt;i class="fa fa-dollar"&gt;&lt;/i&gt;&lt;/span&gt;
                      &lt;input type="text" th:field="*{valor}" class="form-control"&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="col-md-6"&gt;
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Tipo Despesa&lt;/label&gt; 
                      &lt;select class="form-control select2" th:field="*{tipoDespesa}" style="width: 100%;"&gt;
                      &lt;option th:each="tipo : ${tiposDespesa}" th:value="${tipo}" th:text="${tipo}"&gt;&lt;/option&gt;
                  &lt;/select&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Forma Pagamento&lt;/label&gt; 
                      &lt;select class="form-control select2" th:field="*{formaPagamento}" style="width: 100%;"&gt;
                      &lt;option th:each="forma : ${formasPagamento}" th:value="${forma}" th:text="${forma}"&gt;&lt;/option&gt;
                  &lt;/select&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="col-md-12"&gt;
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Observação&lt;/label&gt; 
                  &lt;input type="text" th:field="*{observacao}" class="form-control"&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
   &lt;/div&gt;
   &lt;div class="box-footer"&gt;
      &lt;button type="submit" class="btn btn-primary"&gt;Salvar&lt;/button&gt;
      &lt;a class="btn btn-default" th:href="@{/}"&gt;Cancelar&lt;/a&gt;
   &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>Data Table Where I select the object for editing</strong></p>
  
  <pre><code>&lt;table id="example2" class="table table-bordered table-hover"&gt;
      &lt;thead&gt;
        &lt;tr&gt;
          &lt;th&gt;Data&lt;/th&gt;
          &lt;th&gt;Valor&lt;/th&gt;
          &lt;th&gt;Tipo Despesa&lt;/th&gt;
          &lt;th&gt;Forma Pagamento&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
        &lt;tr th:each="obj : ${despesas}"&gt;
          &lt;td data-title="Data" th:text="${#calendars.format(obj.dataDespesa, 'dd/MM/yyyy HH:mm:ss')}"&gt;&lt;/td&gt;
          &lt;td data-title="Valor" th:text="${#numbers.formatCurrency(obj.valor)}"&gt;&lt;/td&gt;
          &lt;td data-title="Tipo Despesa" th:text="${obj.tipoDespesa}"&gt;&lt;/td&gt;
          &lt;td data-title="Forma Pagamento" th:text="${obj.formaPagamento}"&gt;&lt;/td&gt;
          &lt;td&gt;&lt;a th:href="@{/despesas/edit{id} (id=${obj.codigoDespesa})}"&gt;&lt;i class="glyphicon glyphicon-pencil"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>