0

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);

    }
}
piradian
  • 414
  • 7
  • 19
  • Have you checked if birth date is not null when it arrives in the controller? – NiVeR Jul 02 '18 at 18:54
  • i put something like this : @Column(name = "birth_date" ,nullable=false) in my entity but it didnt work – Jakub Madej Jul 02 '18 at 18:59
  • Which ORM framework are you using, i.e., hibernate? The reason I ask because hibernate doesn't support `LocalDateTime` and you need to implement a data type for this to work. – Dorado Jul 02 '18 at 19:07
  • Yes i am using hibernate . So i try to change LocalDataTime to Data . Or .. Can i convert it to String ? – Jakub Madej Jul 02 '18 at 19:09
  • The same problem. Maybe i have bad name of field in teamplate ? Idk.. – Jakub Madej Jul 02 '18 at 19:12
  • You just presented a good test case. try and convert the data to a string and verify the exception is not causing any issue. Then, if that is the case, create a data type for Hibernate to deal with the type as mentioned in this post. https://stackoverflow.com/questions/27750026/java-8-localdatetime-and-hibernate-4 – Dorado Jul 02 '18 at 19:17
  • i get this now: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.hibernate.samples.type.LocalDateTimeUserType] – Jakub Madej Jul 02 '18 at 19:24
  • Maybe i should give you my git .. maybe i have exception somewhere else.. – Jakub Madej Jul 02 '18 at 19:25
  • This is my app. can u check it ? I know i have all in models, this is my mistake and i must to repair it. https://github.com/kubcio1906/FatherAndChild – Jakub Madej Jul 02 '18 at 19:33
  • if you have `@ModelAttribute` annotation why do you duplicate the data with `@RequestParam` – piradian Jul 02 '18 at 19:58
  • My bad. I fixed it . – Jakub Madej Jul 02 '18 at 20:01

1 Answers1

0

Hibernate throws an exception because you're passing null as a value for one of the mandatory columns/fields.

In turn it became null because spring wasn't able to find a correct setter in model for your attribute name. You see: birth_date and birthDate are two different things for spring mvc.

You can fix this in two ways:

  1. Provide additional setters for your model

    class FatherForm {
        public void setBirth_date(String birthDate) { setBirthDate(birthDate);}
        public void setSecond_Name(String secondName) {setSecondName(secondName);}
        //...
    }
    
  2. construct fatherForm manually from @RequestParam's:

    public String addFather(@RequestParam("first_name") String firstName,
                                @RequestParam("birth_date") String birthDate,
                                @RequestParam("second_name") String secondName,
                                @RequestParam("pesel") String pesel) {
            final FatherForm fatherForm = new FatherForm();
            fatherForm.setFirstName(firstName);
            fatherForm.setSecondName(secondName);
            fatherForm.setBirthDate(birthDate);
            fatherForm.setPesel(pesel);
            //...
            return "AddChild";
    }
    
Bohdan Levchenko
  • 3,411
  • 2
  • 24
  • 28
  • It works :) but .. when i click button "Add" it going to another template as it should be but when I go to my database i see empty table... ehh :D – Jakub Madej Jul 02 '18 at 19:59
  • @JakubMadej try to mark you `addFatherToDB` method with `@Transactional` annotation or maybe even entire class `FatherService`. I guess it would help – Bohdan Levchenko Jul 02 '18 at 20:03
  • I try to learn how to use Spring and i think i must go to my teacher tomorrow, maybe he will help me with this .. i must have a little braek... :) – Jakub Madej Jul 02 '18 at 20:19