Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[etc] timezone 제거 및 Instant 사용 #20

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}