-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
44280aa
commit dc4798e
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/main/kotlin/kr/galaxyhub/sc/common/domain/PrimaryKeyEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |