Skip to content

Commit

Permalink
feat: ULID PK를 가지는 추상 엔티티 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
seokjin8678 committed Dec 3, 2023
1 parent 44280aa commit dc4798e
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/main/kotlin/kr/galaxyhub/sc/common/domain/PrimaryKeyEntity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package kr.galaxyhub.sc.common.domain

import com.github.f4b6a3.ulid.UlidCreator
import jakarta.persistence.Column
import jakarta.persistence.Id
import jakarta.persistence.MappedSuperclass
import jakarta.persistence.PostLoad
import jakarta.persistence.PostPersist
import java.util.Objects
import java.util.UUID
import org.hibernate.proxy.HibernateProxy
import org.springframework.data.domain.Persistable

@MappedSuperclass
abstract class PrimaryKeyEntity : Persistable<UUID> {

@Id
@Column(columnDefinition = "uuid")
private val id: UUID = UlidCreator.getMonotonicUlid().toUuid()

@Transient
private var _isNew = true

override fun getId(): UUID = id

override fun isNew(): Boolean = _isNew

override fun equals(other: Any?): Boolean {
if (other == null) {
return false
}
if (other !is HibernateProxy && this::class != other::class) {
return false
}
return id == getIdentifier(other)
}

private fun getIdentifier(obj: Any): Any {
return when (obj) {
is HibernateProxy -> obj.hibernateLazyInitializer.identifier
else -> (obj as PrimaryKeyEntity).id
}
}

override fun hashCode(): Int = Objects.hashCode(id)

@PostPersist
@PostLoad
protected fun load() {
_isNew = false
}
}

0 comments on commit dc4798e

Please sign in to comment.