I have a class named Tabel that has an person. The classes Tabel and Person have a one-to-many relationship.
@Entity
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "tabel")
public class Tabel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;
private String comments;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "person_id")
private Person person;
}
@Entity
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String lastname;
private String surname;
}
There are also DTOs
@Data
@Builder
@EqualsAndHashCode
@Jacksonized
public class TabelDto implements Serializable {
private Long id;
private String comments;
private PersonDto person;
}
@Value
@Builder
public class PersonDto implements Serializable {
Long id;
String name;
String lastname;
String surname;
}
And my controller
@PostMapping("/addTabel")
public String addTabel(@ModelAttribute TabelDto tabelDto) {
tabelService.save(tabelDto);
return "redirect:/addTabel";
}
@GetMapping("/addTabel")
public String addTabel(Model model) {
TabelDto tabelDto = TabelDto.builder().build();
model.addAttribute("persons", personService.findAllPerson());
model.addAttribute("tabelDTO", tabelDto);
return "addTabel";
}
My HTML Thymeleaf
<form action="#" th:action="@{/tabel/addTabel}" method="post" th:object="${tabelDTO}">
<label>person:</label>
<label>
<select th:field="*{person}" class="form-control">
<option th:each="person: ${persons}" th:value="${person.id}" th:text="${person.name}"></option>
</select>
</label>
<button type="submit">Register</button>
</form>
When I try to send a POST, I get an error.
[tabel] [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MethodArgumentNotValidException: Validation failed for argument [0] in public java.lang.String by.gus.tabel.controller.TabelController.addTabel(by.gus.tabel.dto.TabelDto): [Field error in object 'tabelDto' on field 'person': rejected value [1]; codes [typeMismatch.tabelDto.person,typeMismatch.person,typeMismatch.by.gus.tabel.dto.PersonDto,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [tabelDto.person,person]; arguments []; default message [person]]; default message [Failed to convert value of type 'java.lang.String' to required type 'by.gus.tabel.dto.PersonDto'; Cannot convert value of type 'java.lang.String' to required type 'by.gus.tabel.dto.PersonDto' for property 'person': no matching editors or conversion strategy found]] ]
But if you put the Table class instead of the TableDTO
public String addTabel(@ModelAttribute Tabel tabelDto)
then everything starts working, but I would like to work through the DTO, and not directly with the entity.
How can I solve this?
New contributor
Сяргей Пяшэвіч is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.