0
I am trying to submit a form that in one of the classes there is a relationship, I would like to know how to do this, because every time I try to submit I get the error of "Failed to Convert Property value of type 'java.lang.String'"
Below is my model
@Entity
public class Musica{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull(message = "Campo está vazio ou nulo.")
@Size(min = 2, max = 50)
@Column(nullable = false, name = "titulo", length = 50)
private String titulo;
@Enumerated(EnumType.STRING)
private TipoMusica generoMusica;
@ManyToOne
@JoinColumn(name = "id_playlist_fk")
private Playlist playlist;
@ManyToOne
@JoinColumn(name = "id_artista_fk")
private Artista artista;
My form in the view:
<form class="form-horizontal" action="/musicas/Adicionar-Musica" th:object="${musica}" method="post">
<div class="row">
<div class="form-group col-md-4 offset-md-4">
<div class="alert alert-danger" role="alert" th:if="${#fields.hasErrors('titulo')}">
<label class="validation-message" th:errors="*{titulo}"></label>
</div>
<label class="lbl">Nome da Música</label>
<input type="text" class="form-control" th:field="*{titulo}" placeholder="Ex: Por baixo ou Por Cima de Mim..">
</div>
</div>
<div class="row">
<div class="form-group col-md-4 offset-md-4">
<div class="alert alert-danger" role="alert" th:if="${#fields.hasErrors('artista')}">
<label class="validation-message" th:errors="*{artista}"></label>
</div>
<label class="lbl" >Artista/Banda</label>
<input type="text" class="form-control" th:field="*{artista}" placeholder="Ex: Henrique e Juliano">
</div>
</div>
<a class="btn btn-primary mr-2 mt-2" href="/"><i class="fas fa-angle-left"></i> VOLTAR</a>
<button type="submit" class="btn btn-success mt-2"><i class="fas fa-save"></i> SALVAR INFORMAÇÕES</button>
</form>
Finally my controller:
@PostMapping("/Adicionar-Musica")
public ModelAndView inserirMusica(@Valid Musica musica, BindingResult br) {
ModelAndView mv = new ModelAndView();
if(br.hasErrors()) {
mv.setViewName("musicas/adicionar-musica");
mv.addObject("musica");
}else {
mv.setViewName("redirect:/musicas/lista-musicas");
repositoriomusica.save(musica);
}
return mv;
}
The error that comes to me when trying to save the information:
Failed to convert property value of type java.lang.String to required type br.com.neo.springmusic.model.Artista for property artista; nested exception is java.lang.IllegalStateException: Cannot convert value of type java.lang.String to required type br.com.neo.springmusic.model.Artista for property artista: no matching editors or conversion strategy found
I would like to know how to save an artist without getting the conversion error?
Put the Stacktrace of error, pf.
– Gustavo
ready put the return that appears when trying to save
– neo