Each persistent class should:
XdEntity
.jetbrains.exodus.entitystore.Entity
XdEntityType
class XdUser(entity: Entity) : XdEntity(entity) {
companion object : XdNaturalEntityType<XdUser>()
}
Xodus-DNQ support inheritance. Inherited persistent class still should have a single argument constructor accepting
Entity
and a companion object of type XdEntityType
. But it can have other persistent class as a super-type.
abstract class BaseEntity(entity: Entity) : XdEntity(entity) {
companion object : XdNaturalEntityType<BaseEntity>()
val propertyOfBaseClass by xdStringProp()
}
class SubEntity(entity: Entity) : BaseEntity(entity) {
companion object : XdNaturalEntityType<SubEntity>()
}
For immutable dictionaries it is handy to use persistent enumerations. Elements of persistent enumeration are automatically created and updated on entity meta-model initialization.
class State(entity: Entity) : XdEnumEntity(entity) {
companion object : XdEnumEntityType<State>() {
val OPEN by enumField { title = "open" }
val IN_PROGRESS by enumField { title = "in progress" }
val CLOSED by enumField { title = "closes" }
}
var title by xdRequiredStringProp(unique = true)
}
class Issue(override val entity: Entity): XdEntity() {
companion object : XdNaturalEntityType<Issue>()
val state by xdLink(State)
}
There could be singleton entities. It means that exactly one instance of such entity should exist in a database. For example you could store application settings in such entity.
class TheKing(entity: Entity) : XdEntity(entity) {
companion object : XdSingletonEntityType<TheKing>() {
override fun TheKing.initSingleton() {
name = "Elvis"
}
}
var name by xdRequiredStringProp()
}
fun getKing(store: TransientEntityStore): TheKing {
return store.transactional {
TheKing.get()
}
}