I want to add "Father" to my database. In my database i have fields like: id- type int, first_name - type varchar, last_name - type varchar, birth_date - type date.
I did FatherEntity with this fields. In Service i did method which create new Father and i @Autowired it to my FatherController. I used GETMapping to show form and it works but when i click button "Add" i have exception like:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'birth_date' cannot be null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_161]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_161]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_161]
I dont know why he didn't get birth_date from form. In entity i have bad type of field or what ?
@Entity
@Table(name="t_father")
@Data
public class FatherEntity {
@Id
@GeneratedValue
private int id;
private String firstName;
@Column(name = "birth_date")
private LocalDateTime birthDate;
private String secondName;
private String pesel;
and this is my controller:
@Controller
public class AddFatherToFamily {
@Autowired
FatherService fatherService;
@GetMapping("addFather")
public String addFather(){
return "AddFather";
}
@PostMapping("addFather")
public String addFather(Model model, @RequestParam("first_name") String firstName,
@RequestParam("birth_date") String birthDate,
@RequestParam("second_name") String secondName,
@RequestParam("pesel") String pesel,
@ModelAttribute FatherForm fatherForm){
fatherService.addFatherToDB(fatherForm);
return "AddChild";
}
}
and Service:
@Service
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "session")
public class FatherService {
@Autowired
FatherRespository fatherRespository;
public void addFatherToDB(FatherForm fatherForm){
FatherEntity newFather = new FatherEntity();
newFather.setFirstName(fatherForm.getFirstName());
newFather.setSecondName(fatherForm.getSecondName());
newFather.setBirthDate(fatherForm.getBirthDate());
fatherRespository.save(newFather);
}
}