objectbox-java
objectbox-java copied to clipboard
Support Kotlin inline classes for ids
Inline classes are a good way to add compile time type safety to database queries. They would prevent error caused by querying a box with the wrong id type.
@Entity
data class EntityA(
val str: String,
val id: EntityAId
)
inline class EntityAId(val value: Long)
@Entity
data class EntityB(
val str: String,
val id: EntityBId
)
inline class EntityBId(val value: Long)
val unsafeId: Long = getSomeId() //returns an id meant for EntityA
boxStore.boxFor<EntityB>()[unsafeId] //Runtime error
val safeId: EntityAId = getSomeId()
boxStore.boxFor<EntityB>()[safeId] //Compile time error
And the unsafe call only crashes if you're lucky, other times it might just seem like the database is returning garbage data, since you queried it with the wrong ids.
Thanks for the suggestion. Instead of changing the type of id, which I think is probably an extreme change, did you look into solving this using extension functions?
-Uwe