1
I have the following problem, I have an Entity called Teacher and another called Class, Teacher is with @Onetomany relationship for Class and when I try to save the classes marked in my checkbox in View is presented the following error:
Error During Execution of Processor 'org.thymeleaf.spring4.processor.attr.Springinputcheckboxfieldattrprocessor'
java.lang.Numberformatexception: For input string: "Quintoa"
The Entity Professor is presented below:
@Entity
public class Professor {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "Nome é obrigatório")
@Column(length = 100, nullable = false)
private String nome;
@NotBlank(message = "CPF é obrigatório")
@NaturalId
@Column(length = 15, nullable = false)
private String cpf;
private PosGraduacao posGraduacao;
@Column(name = "desc_pos_graduacao", length = 150)
private String descricaoPosGraduacao;
@OneToMany(mappedBy="professor")
private Collection<Turma> turmas;
// methods getters and setters ...
}
The Entity Class is presented below:
@Entity
@Table(name = "turma")
public class Turma {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "Nome é obrigatória")
@Size(max = 100, message = "O nome não pode conter mais de 100 caracteres")
@Column(length = 80, nullable = false)
private String nome;
@NotEmpty(message = "Horário inicial é obrigatório")
@Column(length = 6)
private String horarioInicial;
@NotEmpty(message = "Horário final é obrigatório")
@Column(length = 6)
private String horarioFinal;
@Enumerated(EnumType.STRING)
@Column(length = 15, nullable = false)
private Escolaridade escolaridade;
@NotNull(message = "Disciplina obrigatória")
@Enumerated(EnumType.STRING)
@Column(length = 20, nullable = false)
private Disciplina disciplina;
@ManyToOne
@JoinColumn(name = "professor_id")
private Professor professor;
// methods getters and setters ...
}
The Professorcontroller is presented below:
@Controller
@RequestMapping("/professor")
public class ProfessorController {
@Autowired
private TurmaService turmas;
@Autowired
private ProfessorService professores;
@RequestMapping("/novo")
public ModelAndView novo() {
ModelAndView mv = new ModelAndView("CadastroProfessor");
Professor professor = new Professor();
professor.setTurmas(new ArrayList<Turma>());
mv.addObject(professor);
return mv;
}
@RequestMapping(method = RequestMethod.POST)
public String salvar(@Validated Professor professor, Errors errors, RedirectAttributes attributes) {
if (errors.hasErrors()) {
return "CadastroProfessor";
}
professores.salvar(professor);
attributes.addFlashAttribute("mensagem", "Professor salvo com sucesso!");
return "redirect:/professor/novo";
}
The View responsible for selecting classes is shown below:
<div class="form-group" >
<label for="turma" class="col-sm-2 control-label" >Turma que atua</label>
<div class="col-sm-5" >
<label class="checkbox-inline" th:each="turma : ${todasTurmas}" >
<input type="checkbox" id="turma" th:value="${turma}" th:text="${turma.nome}" th:field="*{turmas}" />
</label>
</div>
</div>
Good evening Murilo, I put the id and it worked out, thank you very much. You could say why you should send the id?
– Osmael de Sousa Braga
Because id is the identifier you defined in the Class with @Id.
– Murillo Goulart