728x90
๊ธฐ์กด Auditing์ ์ฌ์ฉํ ๋์๋ ์๋์ ๊ฐ์ด ์ฌ์ฉํ์์ต๋๋ค.
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
public class BaseEntity {
@CreatedDate
@Column(updatable = false)
private LocalDateTime createdDate;
@LastModifiedDate
private LocalDateTime lastModifiedDate;
}
์์ ๊ฒฝ์ฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค์๋ ํญ์ 2022-03-06T14:12:23.0023123 ๊ณผ ๊ฐ์ ํ์์ผ๋ก ์ ์ฅ๋์๋๋ฐ,
์ด๋ฅผ ์ข ๋ ๊น๋ํ format์ผ๋ก ๋ฐ๊พธ๊ณ ์ถ์ ๋๊ฐ ์์ต๋๋ค.
๊ทธ๋ด ๋๋ @PrePersist, @PreUpdate๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ๊ฟ ์ ์์ต๋๋ค.
์์
@Getter
@MappedSuperclass
public class BaseTimeEntity {
@CreatedDate
private String createdDate;
@LastModifiedDate
private String modifiedDate;
@PrePersist
public void onPrePersist(){
this.createdDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
this.modifiedDate = this.createdDate;
}
@PreUpdate
public void onPreUpdate(){
this.modifiedDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"));
}
}
@PrePersiste, @PreUpdate๋ฅผ ์ด๋ค๋ฉด Auditing์ ์ฌ์ฉํ ํ์๊ฐ ์์ผ๋ฏ๋ก @EntityListeners๋ฅผ ์ ๊ฑฐํด์ฃผ์์ต๋๋ค.
728x90