본문 바로가기
Spring/Spring Data JPA

@CreatedDate, @LastModifiedDate

by YellowCow 2022. 11. 5.

@CreatedDate, @LastModifiedDate를 사용하기 위해서는

Spring Boot를 실행시키는 클래스(~~~Application)상단에 @EnableJpaAuditing를 적어줘야 한다

@SpringBootApplication
@EnableJpaAuditing
public class ZamongcampusServerApplication {
	public static void main(String[] args) {
		new SpringApplicationBuilder(ZamongcampusServerApplication.class)
				.properties(APPLICATION_LOCATIONS)
				.run(args);
	}
}

 

그리고 @MappedSuperclass와 @EntityListeners를 추가해준다 

@MappedSuperclass
@EntityListeners(value = {AuditingEntityListener.class})
@Getter
public abstract class BaseEntity {
    @CreatedDate
    private LocalDateTime createdAt;

    @LastModifiedDate
    private LocalDateTime updatedAt;


}

 

@CreatedDate

  • 생성일시를 자동으로 기록하게 해주는 Annotation

@LastModifiedDate

  • 마지막 수정일시를 자동으로 기록하게 해주는 Annotation

 

아래와 같이 사용할 수 있다

@MappedSuperclass
@EntityListeners(value = {AuditingEntityListener.class})
@Getter
public abstract class BaseEntity {
    @CreatedDate
    private LocalDateTime createdAt;

    @LastModifiedDate
    private LocalDateTime updatedAt;


}

댓글