Skip to content

Commit

Permalink
[etc] timezone 제거 및 Instant 사용 (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
dojinyou authored and k-kbk committed Sep 29, 2023
1 parent e0d4ef4 commit 4c03247
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
2 changes: 0 additions & 2 deletions compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ services:
POSTGRES_DB: eatda
POSTGRES_USER: local
POSTGRES_PASSWORD: local
TZ: Asia/Seoul
PGTZ: Asia/Seoul
ports:
- "15432:5432"
restart: always
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mjucow.eatda.common.config

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class JacksonConfiguration {
/**
* 기본 objectMapper 는 java8 이후 시간관련 타입(Instant 등)을 변환하지 못하므로 JavaTimeModule 추가 등록
*/
@Bean
fun objectMapper(): ObjectMapper {
return jacksonObjectMapper().registerModule(JavaTimeModule())
}
}
16 changes: 7 additions & 9 deletions src/main/kotlin/com/mjucow/eatda/domain/common/BaseEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import jakarta.persistence.MappedSuperclass
import jakarta.persistence.PrePersist
import jakarta.persistence.PreUpdate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.Instant

@MappedSuperclass
@EntityListeners(AuditingEntityListener::class)
Expand All @@ -20,30 +19,30 @@ abstract class BaseEntity(
val id: Long = DEFAULT_ID,
) {
@Column(nullable = false, updatable = false)
var createdAt: LocalDateTime = LocalDateTime.now(ZONE_ID)
var createdAt: Instant = Instant.now()
protected set

@Column(nullable = false)
var updatedAt: LocalDateTime = LocalDateTime.now(ZONE_ID)
var updatedAt: Instant = Instant.now()
protected set

@PrePersist
fun initTimeColumns() {
createdAt = LocalDateTime.now(ZONE_ID)
updatedAt = LocalDateTime.now(ZONE_ID)
createdAt = Instant.now()
updatedAt = Instant.now()
}

@PreUpdate
fun updateTimeColumn() {
updatedAt = LocalDateTime.now(ZONE_ID)
updatedAt = Instant.now()
}

override fun equals(other: Any?): Boolean {
if (id == DEFAULT_ID) return false
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as BaseEntity
if (id == DEFAULT_ID || other.id == DEFAULT_ID) return false

return id == other.id
}
Expand All @@ -54,6 +53,5 @@ abstract class BaseEntity(

companion object {
const val DEFAULT_ID = 0L
val ZONE_ID: ZoneId = ZoneId.of("Asia/Seoul")
}
}

0 comments on commit 4c03247

Please sign in to comment.