Hej,
załóżmy, że mam encję
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Car {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String brand;
private String model;
@Enumerated(value=STRING)
private Owner owner;
public enum Owner {
PRIVATE, NON-PRIVATE
}
}
repozytorium
public interface CarRepository extends CrudRepository<Car, Long> {
}
Testowo stworzyłem form, który zawiera TYLKO pole brand.
@RequestMapping(value = "car/edit", method = RequestMethod.POST)
public Model edit(@ModelAttribute Car carForm, BindingResult bindingResult) {
if(bindingResult.hasErrors()) {
return getFormModel(carForm);
}
carRepository.save(carForm);
return carList(new Car());
}
Myślałem, że gdy form nie zawiera wszystkich pól encji lub gdy je zawiera ale są one zdisablowane ( czyli nie lecą postem ) to hibernate wykona update TYLKO tych pól. Tak się nie dzieje i w tym przypadku zeruje mi pozostałe pola. Jeśli np. na polu model dodam adnotację @NotNull to wali błędem, wiadomo, pole to jest ustawione na null.
Pytanie co zmienić w kodzie aby poszło tak jak tego potrzebuję?