Код: Выделить всё
@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;
}
Код: Выделить всё
@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;
}
Код: Выделить всё
@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";
}
Код: Выделить всё
person:
Register
Код: Выделить всё
[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]] ]
Код: Выделить всё
public String addTabel(@ModelAttribute Tabel tabelDto)
Как это решить?
Подробнее здесь: https://stackoverflow.com/questions/786 ... operty-per