trouble_shooting

[ Springboot - h2 ] could not execute statement ... constraint [null] , h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column SQL statement 해결하기

walwal_ 2023. 10. 9. 17:29

 

 

 

Dev , Test DB 분리 후 테스트를 시도하던 중 아래와 같은 오류가 났습니다.

 

org.springframework.dao.DataIntegrityViolationException: could not execute statement [NULL not allowed for column "ID"; SQL statemen ..
insert into ... constraint [null]
---
org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column "ID"; SQL statement:

 

 

 

 

오류 내용을 살펴보면 PK로 설정했던 id 필드가 null 이 허용되지 않는다는 것 같습니다.

 

 

 

 

하지만 아래 그림처럼 누락 없이 정확히 입력했음을 알 수 있습니다.

 

 

 

 

구글 검색 후 원인과 해결 방법을 알 수 있었습니다.

제 경우의 원인은 H2의 IDENTITY 사용 조건때문이였습니다.

 

해결

Entity에 IDENTITY 설정 변경하기

 

기존 Entity 클래스에 설정했던 @GeneratedValue(strategy = GenerationType.IDENTITY) 이 설정을 AUTO로 변경해 줍니다.

(좌)기존 코드 / (우)변경 코드

 

실행 결과 : 테스트 성공!

 

참고한 글

https://stackoverflow.com/questions/39094649/h2-database-null-not-allowed-for-column-id-when-inserting-record-using-jdbcte

 

 

+++ 테스트해보지 않았지만 아래 방법도 있다고 합니다..! 

+++ 참고 : https://selgii.tistory.com/53

 

 

 

감사합니다.