feat: 새로 사용할 Auditing + PK BaseEntity 및 JPA AuditorAwareConfig 추가

This commit is contained in:
이상진 2025-08-27 16:15:56 +09:00
parent 87ad7e9df8
commit aad8d3a4ff
2 changed files with 80 additions and 0 deletions

View File

@ -0,0 +1,25 @@
package roomescape.common.config
import org.slf4j.MDC
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.data.domain.AuditorAware
import org.springframework.data.jpa.repository.config.EnableJpaAuditing
import roomescape.auth.web.support.MDC_MEMBER_ID_KEY
import java.util.*
@Configuration
@EnableJpaAuditing
class JpaConfig {
@Bean
fun auditorAware(): AuditorAware<Long> = MdcAuditorAware()
}
class MdcAuditorAware : AuditorAware<Long> {
override fun getCurrentAuditor(): Optional<Long> {
val memberIdStr = MDC.get(MDC_MEMBER_ID_KEY)
return Optional.ofNullable(memberIdStr.toLongOrNull())
}
}

View File

@ -0,0 +1,55 @@
package roomescape.common.entity
import jakarta.persistence.Column
import jakarta.persistence.EntityListeners
import jakarta.persistence.Id
import jakarta.persistence.MappedSuperclass
import jakarta.persistence.PostLoad
import jakarta.persistence.PrePersist
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.domain.Persistable
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import java.time.LocalDateTime
@MappedSuperclass
@EntityListeners(AuditingEntityListener::class)
abstract class AuditingBaseEntity(
@Id
@Column(name = "id")
private val _id: Long,
@Transient
private var isNewEntity: Boolean = true
) : Persistable<Long> {
@Column(updatable = false)
@CreatedDate
lateinit var createdAt: LocalDateTime
protected set
@Column(updatable = false)
@CreatedBy
var createdBy: Long = 0L
protected set
@Column
@LastModifiedDate
lateinit var updatedAt: LocalDateTime
protected set
@Column
@LastModifiedBy
var updatedBy: Long = 0L
protected set
@PostLoad
@PrePersist
fun markNotNew() {
isNewEntity = false
}
override fun getId(): Long = _id
override fun isNew(): Boolean = isNewEntity
}