Study/Java Spring Boot

java.sql.SQLIntegrityConstraintViolationException: Column '컬럼명' cannot be null

kahaha 2023. 3. 16. 21:10

java.sql.SQLIntegrityConstraintViolationException: Column 'user_id' cannot be null 오류를 해결

 

원인은 필수적으로 입력되어야 하는 컬럼에 null값이 들어갔기 때문이다

 

 

아래 코드에서 optional = false이기 null일 수 없어서 에러가 발생하였다.

@Setter @ManyToOne(optional = false) @JoinColumn(name = "userId")
private UserAccount userAccount; //유저 정보(id) 매핑

 

정확하게 매핑이 될 때까지는 optional=true를 사용해 null값을 허용하도록 하자

@Setter @ManyToOne(optional = true) @JoinColumn(name = "userId") //매핑이 완료되면 필수옵션(false)으로 바꿔줘야함
private UserAccount userAccount; //유저 정보(id) 매핑