Last active
April 25, 2025 00:34
-
-
Save djkeh/e69cc6cb45f9f675e56c5d55f56a5250 to your computer and use it in GitHub Desktop.
Spring Data JPA 에서 공통 auditing field를 만들 때 즐겨 쓰는 설계를 담은 추상 클래스의 코틀린 버전. Spring Boot 3.4.3 에서 테스트 완료.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jakarta.persistence.Column | |
import jakarta.persistence.EntityListeners | |
import jakarta.persistence.MappedSuperclass | |
import org.springframework.data.annotation.CreatedBy | |
import org.springframework.data.annotation.CreatedDate | |
import org.springframework.data.annotation.LastModifiedBy | |
import org.springframework.data.annotation.LastModifiedDate | |
import org.springframework.data.jpa.domain.support.AuditingEntityListener | |
import java.time.LocalDateTime | |
import java.time.ZoneOffset | |
@EntityListeners(AuditingEntityListener::class) | |
@MappedSuperclass | |
abstract class AuditingFields { | |
@CreatedDate | |
@Column(nullable = false, updatable = false) | |
var createdAt: LocalDateTime = DUMMY_DATETIME | |
protected set | |
@LastModifiedDate | |
@Column(nullable = false) | |
var modifiedAt: LocalDateTime = DUMMY_DATETIME | |
protected set | |
@CreatedBy | |
@Column(nullable = false, updatable = false) | |
var createdBy: String = DUMMY_AUDITOR | |
protected set | |
@LastModifiedBy | |
@Column(nullable = false) | |
var modifiedBy: String = DUMMY_AUDITOR | |
protected set | |
override fun toString(): String { | |
return "AuditingFields(createdAt=$createdAt, modifiedAt=$modifiedAt, createdBy='$createdBy', modifiedBy='$modifiedBy')" | |
} | |
companion object { | |
val DUMMY_DATETIME: LocalDateTime = LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC) | |
const val DUMMY_AUDITOR: String = "dummy" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
생성-
프로퍼티는 한 번 만들어진 후 업데이트(update)가 금지된다.DUMMY_DATETIME
은 초기값의 별도 입력 없이 not null 속성을 만들기 위한 트릭이고, 이는 JPA Auditing에 의해 올바르게 덮어쓰여진다.DUMMY_AUDITOR
는 초기값의 별도 입력 없이 not null 속성을 만들기 위한 트릭이고, 이는 JPA Auditing에 의해 올바르게 덮어쓰여진다.dummy
를 초기값으로 사용하였다.AuditorAware
빈을 구현하여 등록해야 한다.@EnableJpaAuditing
을 빈 설정에 등록해야 한다.toString()
에 의해 관찰 가능.