[Spring JPA] @CreatedDate, @LastModifiedDate NULL

2024. 6. 27. 23:08·에러노트

문제

@CreatedDate, @LastModifiedDate를 사용할 때 필드의 값이 NULL이 된다.

 

해결

1. Application 파일에 @EnableJpaAuditing 어노테이션 추가

@SpringBootApplication
@EnableJpaAuditing
public class FoundationApplication {

	public static void main(String[] args) {
		SpringApplication.run(FoundationApplication.class, args);
	}

}

 

EnableJpaAuditing

  • 엔티티 생성, 변경 시 값을 주입해야 하는 번거로움을 해소하기 위해 Spring Data JPA에서 값을 자동으로 넣어주는 기능을 제공한다.

 

2. @CreatedDate, @LastModifiedDate 를 선언한 Entity에 @EntityListeners(AuditingEntityListener.class) 어노테이션 추가 -> 해당 클래스에 Auditing 기능을 포함

@Entity
@EntityListeners(AuditingEntityListener.class)
public class Post {
    @CreatedDate
    @Column(name = "created_date")
    private LocalDateTime createdDate;
    
    @LastModifiedDate
    @Column(name = "modified_date")
    private LocalDateTime modifiedDate;
}

 

DB

 

값이 잘 들어간 것을 확인할 수 있다!

 

 

참고

[Spring] JPA Auditing 적용하기

'에러노트' 카테고리의 다른 글

[Spring] Redis를 이용하여 조회수 동시성 이슈 해결  (0) 2025.07.04
[Spring] 좋아요 동시성 문제 해결하기 (feat. synchronized, 비관적 락, 낙관적 락)  (0) 2025.06.28
[Spring] Type definition error: [simple type, class ~]  (0) 2025.01.07
[Spring] Name for argument of type [java.lang.Long] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag.  (4) 2024.10.06
[Gradle] Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.  (5) 2024.10.03
'에러노트' 카테고리의 다른 글
  • [Spring] 좋아요 동시성 문제 해결하기 (feat. synchronized, 비관적 락, 낙관적 락)
  • [Spring] Type definition error: [simple type, class ~]
  • [Spring] Name for argument of type [java.lang.Long] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the '-parameters' flag.
  • [Gradle] Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
starmk
starmk
공부한 것을 정리합니다
  • starmk
    star의 블로그
    starmk
  • 전체
    오늘
    어제
    • 분류 전체보기 (67) N
      • Spring (8) N
      • Algorithm (30) N
        • 백준 (27) N
      • 자료구조 (7) N
      • 에러노트 (6)
      • AWS (2)
        • ACC (6)
      • Jpa (0)
      • 운영체제 (3)
      • Database (1)
      • 네트워크 (1)
      • Web (1)
      • Java (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
starmk
[Spring JPA] @CreatedDate, @LastModifiedDate NULL
상단으로

티스토리툴바