Spring Boot Framework
AuditingEntityListener.class
헤르메스의날개
2023. 3. 12. 04:14
728x90
요즘 JPA를 공부하고 있습니다. MyBatis 만 하다가, 개인적으로 공부를 진행하고 있습니다. 하다보니 막히고, 이해가 안되는 부분이 많네요. 그때마다 검색과 삽질로 해결하고 있습니다. 여기 그 삽질의 자취를 남겨봅니다.
개발환경
Spring Boot 2.7.9
H2 2.1.214
p6spy 1.8.1
slf4j 1.7.36
swagger2 2.6.1
lombok
devtools
postgresql
BaseEntity를 상속받은 Entity 를 save() 해도 @CreateDate, @LastModifiedDate 객체가 생성되지 않습니다. 이럴때 찾아봐야 하는 내용은
@EnableJpaAuditing
입니다.
package octopus.entity;
import java.io.Serializable;
import java.time.LocalDateTime;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import lombok.Getter;
/**
* <pre>
* 부모 클래스를 상속받는 자식 클래스에게 매핑 정보만을 제공
* abstract 클래스로 생성해야 합니다.
* </pre>
*/
@Getter
@MappedSuperclass // BaseEntity를 상속한 Entity들은 아래의 필드들을 컬럼으로 인식한다.
@EntityListeners(AuditingEntityListener.class) // Audting(자동으로 값 Mapping) 기능 추가
public abstract class BaseEntity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* 생성자
*
* @CreatedBy // implements AuditorAware<Long>를 구현한 Class를 생성해야 한다.
*/
// private String crtId;
protected String crtId = "admin";
/**
* 생성일자 Entity가 생성될 때 자동으로 시간을 관리함.
*/
@CreatedDate
private LocalDateTime crtDt;
/**
* 수정자
*
* @LastModifiedBy // implements AuditorAware<Long>를 구현한 Class를 생성해야 한다.
*/
// private String mdfId;
protected String mdfId = "admin";
/**
* 수정일자
*/
@LastModifiedDate
private LocalDateTime mdfDt;
}
package octopus;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@EnableJpaAuditing
@SpringBootApplication
public class OctopusBackendApplication {
public static void main(String[] args) {
SpringApplication.run(OctopusBackendApplication.class, args);
}
}
출처 : https://wildeveloperetrain.tistory.com/76
JPA @CreatedDate @LastModifiedDate 생성 시간, 수정 시간이 저장되는 원리
@CreatedDate, @LastModifiedDate 데이터를 저장할 때 '생성된 시간 정보'와 '수정된 시간 정보'는 여러모로 많이 사용되고 또 중요합니다. JPA를 사용하면서 @CreatedDate, @LastModifiedDate를 사용하여 생성된 시
wildeveloperetrain.tistory.com
728x90