본문 바로가기

TroubleShooting/Java

Column cannot be null 오류 해결

728x90

서비스 운영 중에 아래와 같은 오류가 발생했다.

Column 'CREATE_DATETIME' cannot be null

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement at
org.springframework.orhttp://m.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:276) at 
org.springframework.orhttp://m.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:233) at
org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:566) at


해당 테이블의 필드값을 확인해보니, not null 이지만 default 로 CURRENT_TIMESTAMP 값을 설정하고 있다.


테이블 스키마는 맞게 되어 있는거 같은데 어떻게 된일인지 코드를 다시 살펴보니깐,

jpa 적용하면서 entity 클래스에 해당 필드(CREATE_DATETIME)에 @CreationTimestamp 어노테이션이 없었다.

@Entity
public class Data {
...
@CreationTimestamp
@Column(name = "CREATE_DATETIME", updatable = false)
private LocalDateTime createDatetime;
...

 

어노테이션만 추가하니, "cannot be null" 문제는 해결이 되었다.

 

참고

https://www.baeldung.com/hibernate-creationtimestamp-updatetimestamp