일단 시작.

Timestamped ZonedDateTime 오류 본문

STUDY/Trouble Shooting

Timestamped ZonedDateTime 오류

꾸양! 2024. 5. 22. 16:46

1. Issue Description


  • 발생한 예외
org.springframework.dao.InvalidDataAccessApiUsageException:
Cannot convert unsupported date type java.time.LocalDateTime to java.time.ZonedDateTime;
Supported types are [java.time.LocalDateTime, java.time.LocalDate, java.time.LocalTime, 
java.time.Instant, java.util.Date, java.lang.Long, long]

 

2. 원인 추론


  • ServiceImpl 다음에 터진 예외였고, ZonedDateTime이 언급되어 있으므로 Auditing에서 문제가 생겼을 거라고 판단
  • jpa zoneddatetime 검색

 

3. 해결


  1. ZonedDateTimeLocalDateTime으로 타입 변경하거나
  2. @CreatedDate@LastModifiedDate 기능을 사용하지 않고 @PrePersist@PreUpdate 어노테이션을 이용해서 구현하는 것

ZonedDateTime 타입을 유지하기 위하여 2번 방법을 이용해보기로 했다.

  • 원래 코드
@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Timestamped {

    @CreatedDate()
    @Column(updatable = false)
    private ZonedDateTime createdAt;
}
  • 수정 코드
@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Timestamped {
    @Column(updatable = false)
    private ZonedDateTime createdAt;

    @PrePersist                          // Entity가 INSERT 되기 전에 원하는 메서드 실행
    public void prePersist() {
        this.createdAt = ZonedDateTime.now();
    }
}

 

4. 결과


createdAt 등록 완료!