title: springboot + thymeelaf form handling with select and object
body: <p>Hello there,
                 I'm facing a problem - for several days now- that is: I need to save a form with a select/option that contains a reference  to another class...but when the controller tries to save it fails. The console shows me nothing...any tips? 
          Whitelabel Error Page</p>
  
  <pre><code>This application has no explicit mapping for /error, so you are seeing this as a fallback.
  
  Sun Dec 11 17:00:10 BRST 2016
  There was an unexpected error (type=Bad Request, status=400).
  Validation failed for object='consulta'. Error count: 1
  </code></pre>
  
  <p>The form cadastro.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns:th="http://www.thymeleaf.org"&gt;
  
  &lt;head&gt;
  ...
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;div th:replace="includes/header :: menu"&gt;&lt;/div&gt;
      &lt;div id="consu" class="container"&gt;
  
      &lt;form action="#" th:action="@{salvar}" th:object="${consulta}" method="POST"&gt;
      &lt;div class="form-inline"&gt;
      &lt;label for="select-medico-consulta" class="form-group"&gt;Medico&lt;/label&gt;
          &lt;select  id="select-medico-consulta" th:field="*{medico}"&gt;
              &lt;div&gt;
              &lt;option th:each="medico : ${medicos}" th:value="${medico}"
                          th:text="${medico.nome}" &gt;&lt;/option&gt;
              &lt;/div&gt;          
          &lt;/select&gt;
  
          &lt;div class="form-group"&gt;
           &lt;label id="paciente-label" for="select-paciente-consulta" &gt; Paciente&lt;/label&gt;
          &lt;/div&gt;                      
          &lt;/div&gt;
  
     &lt;div class="form-group"&gt;
    &lt;label for="comment"&gt;Consulta&lt;/label&gt;
    &lt;textarea class="form-control" rows="5" id="comment"&gt;&lt;/textarea&gt;
      &lt;/div&gt; 
  
    &lt;div class="checkbox"&gt;
      &lt;label&gt;
        &lt;input type="checkbox"/&gt; Internado 
      &lt;/label&gt;
    &lt;/div&gt;
  
  
    &lt;button type="submit" class="btn btn-default"&gt;Salvar&lt;/button&gt;
  &lt;/form&gt;
  &lt;/div&gt;    
  
  
  &lt;div th:fragment="footer"&gt;
  
          &lt;p class="rodape"&gt;
              &lt;img class="img-responsive" src="/img/logo.jpg"
                  th:src="@{/img/logo.jpg}" alt="error" /&gt;
          &lt;/p&gt;
  
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>ConsultaController</p>
  
  <pre><code>@Controller
  @RequestMapping("/medclin")
  public class ConsultaController {
  
      @Autowired
      private ConsultaDao consultadao;
  
      @Autowired
      private MedicoDao medicoDao;
  
      @Autowired
      private PacienteDao pacienteDao;
  
  
  
      @RequestMapping("/consulta")
      public ModelAndView Consulta() {
  
          ModelAndView modelAndView = new ModelAndView("consulta/consulta");
  
          ArrayList&lt;Medico&gt; medicos = (ArrayList&lt;Medico&gt;) medicoDao.findAll();
  
          ArrayList&lt;Paciente&gt; pacientes = (ArrayList&lt;Paciente&gt;) pacienteDao.findAll();
  
          modelAndView.addObject("medicos", medicos);
          modelAndView.addObject("pacientes", pacientes);
  
  
          modelAndView.addObject("consulta", new Consulta());
  
  
          return modelAndView;
      }
  
  
  
      @RequestMapping(value = "/salvar", method = RequestMethod.POST)
      public String salvar(@ModelAttribute Consulta consulta) {
  
  
          consultadao.save(consulta);
  
          return "redirect:medclin/home";
      }
  
  }
  </code></pre>
  
  <p>the Model, consulta.java</p>
  
  <p>@Entity
  public class Consulta {</p>
  
  <pre><code>@Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer id;
  
  @ManyToOne
  private Medico medico;
  
  @ManyToMany
  @JoinTable(name = "CONSULTA_PACIENTES")
  private List&lt;Paciente&gt; pacientes = new ArrayList&lt;&gt;();
  
  
  @ManyToMany
  @JoinTable(name = "CONSULTA_RECEITA")
  private List&lt;Receita&gt; receita = new ArrayList&lt;&gt;();
  
  public Consulta(Medico med, List&lt;Paciente&gt; paciente) {
      this.medico = med;
      this.pacientes = paciente;
  }
  
  public Consulta() {
  
  }
  
  public Consulta(Medico medico2) {
      this.medico = medico2;
  }
  
  public Integer getId() {
      return id;
  }
  
  public void setId(Integer id) {
      this.id = id;
  }
  
  public List&lt;Paciente&gt; getPct() {
      return pacientes;
  }
  
  public void setPct(List&lt;Paciente&gt; pct) {
      this.pacientes = pct;
  }
  
  public Medico getMedico() {
      return medico;
  }
  
  public void setMedico(Medico medico) {
      this.medico = medico;
  }
  
  public List&lt;Receita&gt; getReceita() {
      return receita;
  }
  
  public void setReceita(List&lt;Receita&gt; receita) {
      this.receita = receita;
  }
  
  @Override
  public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((id == null) ? 0 : id.hashCode());
      result = prime * result + ((medico == null) ? 0 : medico.hashCode());
      result = prime * result + ((pacientes == null) ? 0 : pacientes.hashCode());
      result = prime * result + ((receita == null) ? 0 : receita.hashCode());
      return result;
  }
  </code></pre>
  
  <p>//TODO corrigir esse erro</p>
  
  <pre><code>public boolean equals(Consulta cons) {
      if (this.id == cons.getId())
          return true;
      else
      return false;
  }
  
  @Override
  public String toString() {
      return "Consulta [id=" + id + ", medico=" + medico + ", pacientes=" + pacientes + ", receita=" + receita + "]";
  }
  </code></pre>
  
  <p>}</p>