반응형
BaseEntity.kt 설정
Oracle에서 @GeneratedValue(strategy = GenerationType.IDENTITY) 전략이 제대로 지원하지 않음.
지원하는 DB는 ID도 포함 가능
import jakarta.persistence.*
import org.springframework.data.annotation.*
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import java.time.LocalDateTime
@MappedSuperclass
@EntityListeners(AuditingEntityListener::class)
abstract class BaseEntity {
@CreatedBy
@Column(name = "CREATED_BY", updatable = false)
var createdBy: String? = null
@CreatedDate
@Column(name = "CREATED_DATE", updatable = false)
var createdDate: LocalDateTime? = null
@LastModifiedBy
@Column(name = "MODIFIED_BY")
var modifiedBy: String? = null
@LastModifiedDate
@Column(name = "MODIFIED_DATE")
var modifiedDate: LocalDateTime? = null
}
Application.kt
실행에 @EnableJpaAuditing // 추가
@EnableJpaAuditing // 추가
@SpringBootApplication
class MyApp
AuditorAware.kt. 생성
import org.springframework.data.domain.AuditorAware
import org.springframework.stereotype.Component
import java.util.*
@Component
class AuditorAwareImpl : AuditorAware<String> {
override fun getCurrentAuditor(): Optional<String> {
// 실 사용 시 SecurityContextHolder 또는 인증 정보에서 추출
return Optional.of("SYSTEM") // 또는 사용자 ID (SecurityContextHolder().userID())
}
}
Entity에 상속
@Entity
@Table(name = "NOTICE")
class Notice(
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "notice_seq_gen")
@SequenceGenerator(
name = "notice_seq_gen",
sequenceName = "SEQ_NOTICE_ID",
allocationSize = 1
)
val id: Long? = null,
val title: String
) : BaseEntity()
반응형
'skill > Java.Kotlin' 카테고리의 다른 글
Kotlin UTC (3) | 2025.06.12 |
---|---|
Oracle + Kotlin + JPA 연결 (0) | 2025.06.12 |
JPA 에러 JpaSystemException, IdentifierGenerationException (0) | 2024.01.12 |
springboot @Scheduler 다중 서버에서 한번만 실행 : shedlock (1) | 2023.11.21 |
[springboot] yml 과 Properties 설정 (1) | 2023.11.21 |